<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once 'excel_reader2.php';
class Keuangan_mahasiswa extends MX_Controller {
    
  function __construct() {
//    $this->load->library('manimage');
    $this->load->model('frm/mfrm');
    $this->load->library('PHPExcel');
    $this->menu = $this->cek();
  }
  
  function jenis_biaya(){
    $data = $this->global_models->get_query("
      SELECT A.*, B.title AS account
      FROM keuangan_mahasiswa_jenis_biaya AS A
      LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
      ");
    $this->template->build("client/jenis-biaya",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-mahasiswa/jenis-biaya',
            'title'       => lang("keuangan_mahasiswa_jenis_biaya"),
            'data'        => $data,
          ));
    $this->template
      ->set_layout('default')
      ->build("client/jenis-biaya");
  }
  
  function jenis_biaya_kuliah(){
    $data = $this->global_models->get_query("
      SELECT A.*, B.title AS account
      FROM keuangan_mahasiswa_jenis_biaya_kuliah AS A
      LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
      ");
    $this->template->build("client/jenis-biaya-kuliah",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-mahasiswa/jenis-biaya-kuliah',
            'title'       => lang("keuangan_mahasiswa_jenis_biaya_kuliah"),
            'data'        => $data,
          ));
    $this->template
      ->set_layout('default')
      ->build("client/jenis-biaya-kuliah");
  }
  
  function jenis_biaya_masuk(){
    $data = $this->global_models->get_query("
      SELECT A.*, B.title AS account
      FROM keuangan_mahasiswa_jenis_biaya_masuk AS A
      LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
      ");
    $this->template->build("client/jenis-biaya-masuk",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-mahasiswa/jenis-biaya-masuk',
            'title'       => lang("keuangan_mahasiswa_jenis_biaya_masuk"),
            'data'        => $data,
          ));
    $this->template
      ->set_layout('default')
      ->build("client/jenis-biaya-masuk");
  }
  
  public function add_jenis_biaya($id_keuangan_mahasiswa_jenis_biaya = 0){
    if($this->input->post(NULL)){
      $pst = $this->input->post(NULL);
      if($pst['id_detail']){
        $kirim = array(
            "title"               => $pst['title'],
            "id_keuangan_mahasiswa_jenis_virtual_account" => $pst['id_keuangan_mahasiswa_jenis_virtual_account'],
            "id_frm_account"      => $pst['id_frm_account'],
            "code"                => $pst['code'],
            "sort"                => $pst['sort'],
            "update_by_users"     => $this->session->userdata("id")
        );
        $id_keuangan_mahasiswa_jenis_biaya = $this->global_models->update("keuangan_mahasiswa_jenis_biaya", array("id_keuangan_mahasiswa_jenis_biaya" => $pst['id_detail']),$kirim);
      }
      else{
        $kirim = array(
            "title"               => $pst['title'],
            "id_keuangan_mahasiswa_jenis_virtual_account" => $pst['id_keuangan_mahasiswa_jenis_virtual_account'],
            "id_frm_account"      => $pst['id_frm_account'],
            "code"                => $pst['code'],
            "sort"                => $pst['sort'],
            "create_by_users"     => $this->session->userdata("id"),
            "create_date"         => date("Y-m-d H:i:s")
        );
        $id_keuangan_mahasiswa_jenis_biaya = $this->global_models->insert("keuangan_mahasiswa_jenis_biaya", $kirim);
      }
      if($id_keuangan_mahasiswa_jenis_biaya){
        $this->session->set_flashdata('success', 'Data tersimpan');
      }
      else{
        $this->session->set_flashdata('notice', 'Data tidak tersimpan');
      }
      redirect("keuangan-mahasiswa/jenis-biaya");
    }
    else{
      $detail = $this->global_models->get("keuangan_mahasiswa_jenis_biaya", array("id_keuangan_mahasiswa_jenis_biaya" => $id_keuangan_mahasiswa_jenis_biaya));
      $this->template->build("client/add-jenis-biaya",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'keuangan-mahasiswa/jenis-biaya',
              'title'       => lang("keuangan_mahasiswa_jenis_biaya"),
              'detail'      => $detail,
            ));
      $this->template
        ->set_layout('default')
        ->build("client/add-jenis-biaya");
    }
	}
  
  public function add_jenis_biaya_kuliah($id_keuangan_mahasiswa_jenis_biaya_kuliah = 0){
    if($this->input->post(NULL)){
      $pst = $this->input->post(NULL);
      if($pst['id_detail']){
        $kirim = array(
            "title"               => $pst['title'],
            "id_keuangan_mahasiswa_jenis_virtual_account" => $pst['id_keuangan_mahasiswa_jenis_virtual_account'],
            "id_frm_account"      => $pst['id_frm_account'],
            "code"                => $pst['code'],
            "sort"                => $pst['sort'],
            "update_by_users"     => $this->session->userdata("id")
        );
        $id_keuangan_mahasiswa_jenis_biaya_kuliah = $this->global_models->update("keuangan_mahasiswa_jenis_biaya_kuliah", array("id_keuangan_mahasiswa_jenis_biaya_kuliah" => $pst['id_detail']),$kirim);
      }
      else{
        $kirim = array(
            "title"               => $pst['title'],
            "id_keuangan_mahasiswa_jenis_virtual_account" => $pst['id_keuangan_mahasiswa_jenis_virtual_account'],
            "id_frm_account"      => $pst['id_frm_account'],
            "code"                => $pst['code'],
            "sort"                => $pst['sort'],
            "create_by_users"     => $this->session->userdata("id"),
            "create_date"         => date("Y-m-d H:i:s")
        );
        $id_keuangan_mahasiswa_jenis_biaya_kuliah = $this->global_models->insert("keuangan_mahasiswa_jenis_biaya_kuliah", $kirim);
      }
      if($id_keuangan_mahasiswa_jenis_biaya_kuliah){
        $this->session->set_flashdata('success', 'Data tersimpan');
      }
      else{
        $this->session->set_flashdata('notice', 'Data tidak tersimpan');
      }
      redirect("keuangan-mahasiswa/jenis-biaya-kuliah");
    }
    else{
      $detail = $this->global_models->get("keuangan_mahasiswa_jenis_biaya_kuliah", array("id_keuangan_mahasiswa_jenis_biaya_kuliah" => $id_keuangan_mahasiswa_jenis_biaya_kuliah));
      $this->template->build("client/add-jenis-biaya-kuliah",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'keuangan-mahasiswa/jenis-biaya',
              'title'       => lang("keuangan_mahasiswa_jenis_biaya_kuliah"),
              'detail'      => $detail,
            ));
      $this->template
        ->set_layout('default')
        ->build("client/add-jenis-biaya-kuliah");
    }
	}
  
  public function add_jenis_biaya_masuk($id_keuangan_mahasiswa_jenis_biaya_masuk = 0){
    if($this->input->post(NULL)){
      $pst = $this->input->post(NULL);
      if($pst['id_detail']){
        $kirim = array(
            "title"               => $pst['title'],
            "id_keuangan_mahasiswa_jenis_virtual_account" => $pst['id_keuangan_mahasiswa_jenis_virtual_account'],
            "id_frm_account"      => $pst['id_frm_account'],
            "code"                => $pst['code'],
            "sort"                => $pst['sort'],
            "update_by_users"     => $this->session->userdata("id")
        );
        $id_keuangan_mahasiswa_jenis_biaya_masuk = $this->global_models->update("keuangan_mahasiswa_jenis_biaya_masuk", array("id_keuangan_mahasiswa_jenis_biaya_masuk" => $pst['id_detail']),$kirim);
      }
      else{
        $kirim = array(
            "title"               => $pst['title'],
            "id_keuangan_mahasiswa_jenis_virtual_account" => $pst['id_keuangan_mahasiswa_jenis_virtual_account'],
            "id_frm_account"      => $pst['id_frm_account'],
            "code"                => $pst['code'],
            "sort"                => $pst['sort'],
            "create_by_users"     => $this->session->userdata("id"),
            "create_date"         => date("Y-m-d H:i:s")
        );
        $id_keuangan_mahasiswa_jenis_biaya_masuk = $this->global_models->insert("keuangan_mahasiswa_jenis_biaya_masuk", $kirim);
      }
      if($id_keuangan_mahasiswa_jenis_biaya_masuk){
        $this->session->set_flashdata('success', 'Data tersimpan');
      }
      else{
        $this->session->set_flashdata('notice', 'Data tidak tersimpan');
      }
      redirect("keuangan-mahasiswa/jenis-biaya-masuk");
    }
    else{
      $detail = $this->global_models->get("keuangan_mahasiswa_jenis_biaya_masuk", array("id_keuangan_mahasiswa_jenis_biaya_masuk" => $id_keuangan_mahasiswa_jenis_biaya_masuk));
      $this->template->build("client/add-jenis-biaya-masuk",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'keuangan-mahasiswa/jenis-biaya-masuk',
              'title'       => lang("keuangan_mahasiswa_jenis_biaya_masuk"),
              'detail'      => $detail,
            ));
      $this->template
        ->set_layout('default')
        ->build("client/add-jenis-biaya-masuk");
    }
	}
  
  public function setting_biaya_angkatan($id_kelompok_mahasiswa, $course_category = 0){
    $this->global_models->get_connect("zikula");
    $list_course_category = $this->global_models->get_dropdown("academicportal_course_categories", "id", "name", FALSE, 
            array("set_as_program_jurusan >" => 0));
    $this->global_models->get_connect("default");
    if($this->input->post(NULL)){
      $pst = $this->input->post(NULL);
      
      $list_select_course_category = $pst['course_category_multi'];
      if($pst['clon_to'] > 0){
        
        $master_biaya_mahasiswa_baru_sks = $this->global_models->get("keuangan_mahasiswa_jenis_biaya_masuk_sks",
                array(
                    "id_course_category"                        => $pst['course_category'],
                    "id_kelompok_mahasiswa"                     => $id_kelompok_mahasiswa
                ));
        
        $master_biaya_mahasiswa_semester = $this->global_models->get("keuangan_mahasiswa_setting_biaya_angkatan", 
                array(
                    "id_course_category"                  => $pst['course_category'],
                    "id_kelompok_mahasiswa"               => $id_kelompok_mahasiswa
                ));
        
        $master_biaya_mahasiswa_cicilan = $this->global_models->get("keuangan_mahasiswa_setting_biaya_angkatan_cicilan", 
                array(
                    "id_course_category"                  => $pst['course_category'],
                    "id_kelompok_mahasiswa"               => $id_kelompok_mahasiswa
                ));
        
        $master_biaya_mahasiswa_cicilan_kuliah = $this->global_models->get("keuangan_mahasiswa_setting_biaya_angkatan_cicilan_kuliah", 
                array(
                    "id_course_category"                  => $pst['course_category'],
                    "id_kelompok_mahasiswa"               => $id_kelompok_mahasiswa
                ));
        
        foreach ($pst['course_category_multi'] AS $cak => $id_cat){
//          delete
          $this->global_models->delete("keuangan_mahasiswa_jenis_biaya_masuk_sks", array(
              "id_course_category"      => $id_cat,
              "id_kelompok_mahasiswa"   => $pst['clon_to'],
          ));
          $this->global_models->delete("keuangan_mahasiswa_setting_biaya_angkatan", array(
              "id_course_category"      => $id_cat,
              "id_kelompok_mahasiswa"   => $pst['clon_to'],
          ));
          $this->global_models->delete("keuangan_mahasiswa_setting_biaya_angkatan_cicilan", array(
              "id_course_category"      => $id_cat,
              "id_kelompok_mahasiswa"   => $pst['clon_to'],
          ));
          $this->global_models->delete("keuangan_mahasiswa_setting_biaya_angkatan_cicilan_kuliah", array(
              "id_course_category"      => $id_cat,
              "id_kelompok_mahasiswa"   => $pst['clon_to'],
          ));
//          delete
          $clon_biaya_mahasiswa_baru_sks[] = array(
              "id_course_category"      => $id_cat,
              "id_kelompok_mahasiswa"   => $pst['clon_to'],
              "price"                   => $master_biaya_mahasiswa_baru_sks[0]->price,
              "sks"                     => $master_biaya_mahasiswa_baru_sks[0]->sks,
              "note"                    => $master_biaya_mahasiswa_baru_sks[0]->note,
              "create_by_users"         => $this->session->userdata("id"),
              "create_date"             => date("Y-m-d H:i:s")
          );
          
          foreach ($master_biaya_mahasiswa_semester AS $mbms){
            $clon_biaya_mahasiswa_semester[] = array(
                "id_kelompok_mahasiswa"                     => $pst['clon_to'],
                "id_keuangan_mahasiswa_jenis_biaya"         => $mbms->id_keuangan_mahasiswa_jenis_biaya,
                "id_keuangan_mahasiswa_jenis_biaya_kuliah"  => $mbms->id_keuangan_mahasiswa_jenis_biaya_kuliah,
                "id_keuangan_mahasiswa_jenis_biaya_masuk"   => $mbms->id_keuangan_mahasiswa_jenis_biaya_masuk,
                "id_course_category"                        => $id_cat,
                "price"                                     => $mbms->price,
                "note"                                      => $mbms->note,
                "create_by_users"                           => $this->session->userdata("id"),
                "create_date"                               => date("Y-m-d H:i:s")
            );
          }
          
          foreach ($master_biaya_mahasiswa_cicilan AS $mbmc){
            $clon_biaya_mahasiswa_cicilan[] = array(
                "id_kelompok_mahasiswa" => $pst['clon_to'],
                "id_course_category"    => $id_cat,
                "type"                  => $mbmc->type,
                "percent"               => $mbmc->percent,
                "create_by_users"       => $this->session->userdata("id"),
                "create_date"           => date("Y-m-d H:i:s")
            );
          }
          
          foreach ($master_biaya_mahasiswa_cicilan_kuliah AS $mbmck){
            $clon_biaya_mahasiswa_cicilan_kuliah[] = array(
                "id_kelompok_mahasiswa" => $pst['clon_to'],
                "id_course_category"    => $id_cat,
                "semester"              => $mbmck->semester,
                "percent"               => $mbmck->percent,
                "create_by_users"       => $this->session->userdata("id"),
                "create_date"           => date("Y-m-d H:i:s")
            );
          }
        }
        
        $this->global_models->insert_batch("keuangan_mahasiswa_jenis_biaya_masuk_sks", $clon_biaya_mahasiswa_baru_sks);
        $this->global_models->insert_batch("keuangan_mahasiswa_setting_biaya_angkatan", $clon_biaya_mahasiswa_semester);
        $this->global_models->insert_batch("keuangan_mahasiswa_setting_biaya_angkatan_cicilan", $clon_biaya_mahasiswa_cicilan);
        $this->global_models->insert_batch("keuangan_mahasiswa_setting_biaya_angkatan_cicilan_kuliah", $clon_biaya_mahasiswa_cicilan_kuliah);
        
//        $this->debug($clon_biaya_mahasiswa_cicilan_kuliah, true);
                
      }
      else{
        foreach ($pst['course_category_multi'] AS $cak => $id_cat){
          $pst['course_category'] = $id_cat;
  //        $this->debug($id_cat, true);
          $kirim = array();
          $kirim_cicilan = array();
          $kirim_cicilan_kuliah = array();
          $this->global_models->delete("keuangan_mahasiswa_setting_biaya_angkatan", array("id_kelompok_mahasiswa" => $pst['id_kelompok_mahasiswa'], 
              "id_course_category" => $pst['course_category']));
    //      die;
          foreach($pst['biaya'] AS $id_keuangan_mahasiswa_jenis_biaya => $price){
            $kirim[] = array(
                "id_kelompok_mahasiswa"             => $pst['id_kelompok_mahasiswa'],
                "id_keuangan_mahasiswa_jenis_biaya" => $id_keuangan_mahasiswa_jenis_biaya,
                "id_keuangan_mahasiswa_jenis_biaya_kuliah" => 0,
                "id_keuangan_mahasiswa_jenis_biaya_masuk" => 0,
                "id_course_category"                => $pst['course_category'],
                "price"                             => $this->global_models->string_to_number($price),
                "create_by_users"                   => $this->session->userdata("id"),
                "create_date"                       => date("Y-m-d H:i:s")
            );
          }
          foreach($pst['biaya_kuliah'] AS $id_keuangan_mahasiswa_jenis_biaya_kuliah => $price){
            $kirim[] = array(
                "id_kelompok_mahasiswa"             => $pst['id_kelompok_mahasiswa'],
                "id_keuangan_mahasiswa_jenis_biaya" => 0,
                "id_keuangan_mahasiswa_jenis_biaya_kuliah" => $id_keuangan_mahasiswa_jenis_biaya_kuliah,
                "id_keuangan_mahasiswa_jenis_biaya_masuk" => 0,
                "id_course_category"                => $pst['course_category'],
                "price"                             => $this->global_models->string_to_number($price),
                "create_by_users"                   => $this->session->userdata("id"),
                "create_date"                       => date("Y-m-d H:i:s")
            );
          }
          foreach($pst['biaya_masuk'] AS $id_keuangan_mahasiswa_jenis_biaya_masuk => $price){
            $kirim[] = array(
                "id_kelompok_mahasiswa"             => $pst['id_kelompok_mahasiswa'],
                "id_keuangan_mahasiswa_jenis_biaya" => 0,
                "id_keuangan_mahasiswa_jenis_biaya_kuliah" => 0,
                "id_keuangan_mahasiswa_jenis_biaya_masuk" => $id_keuangan_mahasiswa_jenis_biaya_masuk,
                "id_course_category"                => $pst['course_category'],
                "price"                             => $this->global_models->string_to_number($price),
                "create_by_users"                   => $this->session->userdata("id"),
                "create_date"                       => date("Y-m-d H:i:s")
            );
          }
    //      $this->debug($kirim, true);
          $id_keuangan_mahasiswa_setting_biaya_angkatan = $this->global_models->insert_batch("keuangan_mahasiswa_setting_biaya_angkatan", $kirim);


    //      cicilan
          $this->global_models->delete("keuangan_mahasiswa_setting_biaya_angkatan_cicilan", array("id_kelompok_mahasiswa" => $pst['id_kelompok_mahasiswa'], 
              "id_course_category" => $pst['course_category']));
          for($g = 0 ; $g <=3 ; $g++){
            $kirim_cicilan[] = array(
                "id_kelompok_mahasiswa"     => $pst['id_kelompok_mahasiswa'],
                "id_course_category"        => $pst['course_category'],
                "type"                      => $g,
                "percent"                   => $this->global_models->string_to_number($pst['percent'][$g]),
                "create_by_users"           => $this->session->userdata("id"),
                "create_date"               => date("Y-m-d H:i:s")
            );
          }

          $id_keuangan_mahasiswa_setting_biaya_angkatan_cicilan = $this->global_models->insert_batch("keuangan_mahasiswa_setting_biaya_angkatan_cicilan", $kirim_cicilan);

    //      cicilan tahunan
          $this->global_models->delete("keuangan_mahasiswa_setting_biaya_angkatan_cicilan_kuliah", array("id_kelompok_mahasiswa" => $pst['id_kelompok_mahasiswa'], 
              "id_course_category" => $pst['course_category']));
          foreach($pst['semester'] AS $semester_ke => $percent_kuliah){
            $kirim_cicilan_kuliah[] = array(
                "id_kelompok_mahasiswa"     => $pst['id_kelompok_mahasiswa'],
                "id_course_category"        => $pst['course_category'],
                "semester"                  => $semester_ke,
                "percent"                   => $percent_kuliah,
                "create_by_users"           => $this->session->userdata("id"),
                "create_date"               => date("Y-m-d H:i:s")
            );
          }
          $id_keuangan_mahasiswa_setting_biaya_angkatan_cicilan_kuliah = $this->global_models->insert_batch("keuangan_mahasiswa_setting_biaya_angkatan_cicilan_kuliah", $kirim_cicilan_kuliah);

          $this->global_models->delete("keuangan_mahasiswa_jenis_biaya_masuk_sks", array("id_kelompok_mahasiswa" => $pst['id_kelompok_mahasiswa'], "id_course_category" => $pst['course_category']));
          $kirim_sks_masuk = array(
              "id_course_category"      => $pst['course_category'],
              "id_kelompok_mahasiswa"   => $pst['id_kelompok_mahasiswa'],
              "price"                   => $this->global_models->string_to_number($pst['sks_awal']),
              "sks"                     => $pst['sks_awal_total'],
              "create_by_users"           => $this->session->userdata("id"),
              "create_date"               => date("Y-m-d H:i:s")
          );

          $this->global_models->insert("keuangan_mahasiswa_jenis_biaya_masuk_sks", $kirim_sks_masuk);

          if($id_keuangan_mahasiswa_setting_biaya_angkatan){
            $this->session->set_flashdata('success', 'Data tersimpan');
          }
          else{
            $this->session->set_flashdata('notice', 'Data tidak tersimpan');
          }
        }
      }
      redirect("keuangan-mahasiswa/setting-biaya-angkatan/".$id_kelompok_mahasiswa."/".$course_category);
    }
    else{
      $this->global_models->get_connect("zikula");
      $angkatan = $this->global_models->get("academicportal_kelompok_mahasiswa", array("id_kelompok_mahasiswa" => $id_kelompok_mahasiswa));
      $list_course_category = $this->global_models->get_dropdown("academicportal_course_categories", "id", "name", TRUE, 
              array("set_as_program_jurusan >" => 0));
      
      $list_kelompok = $this->global_models->get_dropdown("academicportal_kelompok_mahasiswa", "id_kelompok_mahasiswa", "nama_kelompok");
      
      $this->global_models->get_connect("default");
      
//      $jenis_biaya = $this->global_models->get("keuangan_mahasiswa_jenis_biaya");
      
      $jenis_biaya = $this->global_models->get_query("
        SELECT A.*, B.price
        FROM keuangan_mahasiswa_jenis_biaya AS A
        LEFT JOIN keuangan_mahasiswa_setting_biaya_angkatan AS B ON (A.id_keuangan_mahasiswa_jenis_biaya = B.id_keuangan_mahasiswa_jenis_biaya AND B.id_kelompok_mahasiswa = {$id_kelompok_mahasiswa} AND B.id_course_category = {$course_category})
      ");
      
      $jenis_biaya_kuliah = $this->global_models->get_query("
        SELECT A.*, B.price
        FROM keuangan_mahasiswa_jenis_biaya_kuliah AS A
        LEFT JOIN keuangan_mahasiswa_setting_biaya_angkatan AS B ON (A.id_keuangan_mahasiswa_jenis_biaya_kuliah = B.id_keuangan_mahasiswa_jenis_biaya_kuliah AND B.id_kelompok_mahasiswa = {$id_kelompok_mahasiswa} AND B.id_course_category = {$course_category})
      ");
      
      $jenis_biaya_masuk = $this->global_models->get_query("
        SELECT A.*, B.price
        FROM keuangan_mahasiswa_jenis_biaya_masuk AS A
        LEFT JOIN keuangan_mahasiswa_setting_biaya_angkatan AS B ON (A.id_keuangan_mahasiswa_jenis_biaya_masuk = B.id_keuangan_mahasiswa_jenis_biaya_masuk AND B.id_kelompok_mahasiswa = {$id_kelompok_mahasiswa} AND B.id_course_category = {$course_category})
      ");
        
//      $this->debug($jenis_biaya_masuk, true);
        
//      cicilan
      $cicilan = $this->global_models->get_query("
        SELECT * FROM keuangan_mahasiswa_setting_biaya_angkatan_cicilan
        WHERE id_kelompok_mahasiswa = {$id_kelompok_mahasiswa} AND id_course_category = {$course_category}
          ORDER BY type ASC
        ");
        
      $cicilan_kuliah = $this->global_models->get_query("
        SELECT * FROM keuangan_mahasiswa_setting_biaya_angkatan_cicilan_kuliah
        WHERE id_kelompok_mahasiswa = {$id_kelompok_mahasiswa} AND id_course_category = {$course_category}
          ORDER BY semester ASC
        ");
//      $this->debug($cicilan_kuliah, true);
      if($cicilan_kuliah){
        $nomor = 1;
        foreach($cicilan_kuliah AS $ck){
          $isi[] = $this->form_eksternal->form_input("semester[]", $ck->percent);
          $tr[] = "tr_".$nomor;
          $nomor++;
        }
      }
      else{
        $isi[] = $this->form_eksternal->form_input("semester[]", "");
        $tr[] = "tr_1";
        $nomor = 1;
      }
      
      $sks_masuk = $this->global_models->get("keuangan_mahasiswa_jenis_biaya_masuk_sks", array("id_kelompok_mahasiswa" => $id_kelompok_mahasiswa, "id_course_category" => $course_category));
      
//      $this->debug($jenis_biaya_kuliah, true);
      
      $this->template->build("setting-biaya-angkatan",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'keuangan-mahasiswa/setting-biaya',
              'title'       => lang("keuangan_mahasiswa_setting_biaya")." Angkatan ".$angkatan[0]->nama_kelompok,
              'jenis_biaya' => $jenis_biaya,
              'jenis_biaya_kuliah' => $jenis_biaya_kuliah,
              'jenis_biaya_masuk' => $jenis_biaya_masuk,
              'angkatan'    => $angkatan,
              'tr'          => $tr,
              'nomor'       => $nomor,
              'sks_masuk'   => $sks_masuk,
              'course_category'  => $course_category,
              'list_course_category'  => $list_course_category,
              'id_kelompok_mahasiswa' => $id_kelompok_mahasiswa,
              'cicilan'     => $cicilan,
              'isi'         => $isi,
              'list_kelompok' => $list_kelompok
            ));
      $this->template
        ->set_layout('default')
        ->build("setting-biaya-angkatan");
    }
	}
  
  public function payment_mahasiswa($nim, $id_keuangan_mahasiswa_jenis_virtual_account = 0, $id_keuangan_mahasiswa_tanggungan = 0){
    if($this->input->post(NULL)){
      $this->global_models->get_connect("zikula");
      $mahasiswa = $this->global_models->get_query("
        SELECT A.*
        FROM academicportal_student_categories AS A
        LEFT JOIN academicportal_mahasiswa AS B ON A.users = B.pn_uid
        WHERE B.nim = '{$nim}'");
//      $current = $this->global_models->get("academicportal_semester_tahun_ajaran_berjalan", array("is_running" => 1));
      $fakultas = $this->global_models->get_field("academicportal_course_categories", "parent", array("id" => $mahasiswa[0]->course_category));
      $this->global_models->get_connect("default");
      $current = $this->get_current_sta($mahasiswa[0]->course_category);
      
      $pst = $this->input->post(NULL);
      if($pst['id_detail']){
        $kirim = array(
            "title"               => $pst['title'],
            "code"                => $pst['code'],
            "sort"                => $pst['sort'],
            "update_by_users"     => $this->session->userdata("id")
        );
        $id_keuangan_mahasiswa_jenis_biaya = $this->global_models->update("keuangan_mahasiswa_jenis_biaya", array("id_keuangan_mahasiswa_jenis_biaya" => $pst['id_detail']),$kirim);
      }
      else{
        
//        $id_pembayaran_mahasiswa = $this->nbscache->get_explode("variable", "id_pembayaran_mahasiswa");
//        $id_piutang_mahasiswa = $this->nbscache->get_explode("variable", "id_piutang_mahasiswa");
//        $id_hutang_mahasiswa = $this->nbscache->get_explode("variable", "id_hutang_mahasiswa");
        $id_pembayaran_mahasiswa = $this->nbscache->get_explode("variable", "id_pembayaran_mahasiswa");
        $id_piutang_mahasiswa = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_fakultas_beban_mahasiswa_".$fakultas);
        $id_hutang_mahasiswa = $this->nbscache->get_explode("variable", "id_hutang_mahasiswa");
        
//        $total_debit = $this->global_models->get_query("
//          SELECT SUM(A.saldo) AS saldo
//          FROM keuangan_mahasiswa_tanggungan AS A
//          WHERE A.nim = '{$nim}' AND A.pos = 1
//          ");
//
//        $total_kredit = $this->global_models->get_query("
//          SELECT SUM(A.saldo) AS saldo
//          FROM keuangan_mahasiswa_tanggungan AS A
//          WHERE A.nim = '{$nim}' AND A.pos = 2
//          ");
          
        $total_tanggungan = $this->mfrm->get_tunggakan($nim);
//        $this->debug($total_debit);
//        $this->debug("keuangan_mahasiswa_fakultas_beban_mahasiswa_".$fakultas);
//        $this->debug($id_piutang_mahasiswa, true);
        $this->global_models->get_connect("zikula");
        $program_kuliah = $this->global_models->get("academicportal_mahasiswa", array("nim" => $pst['nim']));
        $this->global_models->get_connect("default");
        $kirim = array(
            "nim"                 => $pst['nim'],
            "id_frm_account"      => $id_pembayaran_mahasiswa[1],
            "id_program_kuliah"   => $program_kuliah[0]->program_kuliah,
            "id_kelompok"         => $program_kuliah[0]->kelompok_mahasiswa,
            "id_keuangan_mahasiswa_jenis_virtual_account"      => $pst["id_keuangan_mahasiswa_jenis_virtual_account"],
            "notransaksi"         => $pst['notransaksi'],
            "saldo"               => $this->global_models->string_to_number($pst['saldo']),
            "pos"                 => 2,
            "id_semester"         => $current[0]->semester,
            "tahun"               => $current[0]->tahun_ajaran,
            "tahunsemester"       => $current[0]->tahun_ajaran.$current[0]->semester,
            "note"                => $pst['note'],
            "create_by_users"     => $this->session->userdata("id"),
            "create_date"         => $pst['tanggal']
        );
        $id_keuangan_mahasiswa_tanggungan = $this->global_models->insert("keuangan_mahasiswa_tanggungan", $kirim);
        
        $kirim = array(
            "id_frm_journal"      => $this->mfrm->get_journal_current(),
            "notransaksi"         => $pst['notransaksi'],
            "saldo"               => $this->global_models->string_to_number($pst['saldo']),
            "tanggal"             => $pst['tanggal'],
            "note"                => "Pembayaran Biaya Tanggungan <a href='".site_url("keuangan-mahasiswa/kas-mahasiswa/".$pst['nim'])."'>{$pst['nim']}</a> -- ".$pst['note'],
        );
            
        $id_frm_journal_log = $this->mfrm->create_journal_log($kirim);
        if($id_frm_journal_log){
          if($total_tanggungan <= 0){
            
            $debit[] = $this->global_models->string_to_number($pst['saldo']);
            $debit_da[] = $id_pembayaran_mahasiswa[1];

            $kredit[] = $this->global_models->string_to_number($pst['saldo']);
            $kredit_da[] = $id_hutang_mahasiswa[1];
            $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);
            
          } 
          else{
            if(($total_tanggungan - $this->global_models->string_to_number($pst['saldo'])) >= 0){
              
              $debit[] = $this->global_models->string_to_number($pst['saldo']);
              $debit_da[] = $id_pembayaran_mahasiswa[1];

              $kredit[] = $this->global_models->string_to_number($pst['saldo']);
              $kredit_da[] = $id_piutang_mahasiswa[1];
              $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);

            }
            else{
              
              $debit[] = $this->global_models->string_to_number($pst['saldo']);
              $debit_da[] = $id_pembayaran_mahasiswa[1];

              $kredit[] = abs($total_tanggungan);
              $kredit_da[] = $id_piutang_mahasiswa[1];
              $kredit[] = $this->global_models->string_to_number($pst['saldo']) - $total_tanggungan;
              $kredit_da[] = $id_hutang_mahasiswa[1];
              $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);
              
            }
          }
        }
        
//        cek lunas
        $total_debit = $this->global_models->get_query("
          SELECT SUM(A.saldo) AS saldo
          FROM keuangan_mahasiswa_tanggungan AS A
          WHERE A.nim = '{$nim}' AND A.pos = 1
          ");

        $total_kredit = $this->global_models->get_query("
          SELECT SUM(A.saldo) AS saldo
          FROM keuangan_mahasiswa_tanggungan AS A
          WHERE A.nim = '{$nim}' AND (A.pos = 2 OR A.pos = 3)
          ");
          
//        $pos_asli = $this->global_models->get_field("frm_account", "pos", array("id_frm_account" => $id_frm_account));
//        if($pos_asli == 1)
//          $total_akhir = $total_debit[0]->saldo - $total_kredit[0]->saldo;
//        else
          $total_akhir = $total_kredit[0]->saldo - $total_debit[0]->saldo;  
        
        if($total_akhir >= 0){
//          set keuangan
          $this->global_models->get_connect("zikula");
          $this->global_models->update("academicportal_persyaratan_keuangan", array("uid" => $mahasiswa[0]->users),
                  array("delete_by_who" => -1));
          $this->global_models->get_connect("default");
        }
          
      }
      if($id_keuangan_mahasiswa_tanggungan){
        $this->session->set_flashdata('success', 'Data tersimpan');
      }
      else{
        $this->session->set_flashdata('notice', 'Data tidak tersimpan');
      }
      redirect("keuangan-mahasiswa/kas-mahasiswa/".$pst['nim']);
    }
    else{
      $detail = $this->global_models->get("keuangan_mahasiswa_tanggungan", array("id_keuangan_mahasiswa_tanggungan" => $id_keuangan_mahasiswa_tanggungan));
      $this->template->build("add-payment-mahasiswa",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'keuangan-mahasiswa/mahasiswa',
              'title'       => lang("keuangan_mahasiswa_payment"),
              'detail'      => $detail,
              'nim'         => $nim,
              'id_keuangan_mahasiswa_jenis_virtual_account' => $id_keuangan_mahasiswa_jenis_virtual_account
            ));
      $this->template
        ->set_layout('default')
        ->build("add-payment-mahasiswa");
    }
	}
  
  public function payment_mahasiswa_baru($id_ao_applicant, $id_keuangan_mahasiswa_jenis_virtual_account = 0, $id_keuangan_mahasiswa_baru_tanggungan = 0){
    if($this->input->post(NULL)){
      $this->global_models->get_connect("zikula");
      $mahasiswa = $this->global_models->get_query("
        SELECT A.*
        FROM academicportal_ao_applicant AS A
        WHERE A.id = '{$id_ao_applicant}'");
        
      $schedule = $this->global_models->get("academicportal_ao_schedule", array("id" => $mahasiswa[0]->schedule_id));
//      $current = $this->global_models->get("academicportal_semester_tahun_ajaran_berjalan", array("is_running" => 1));
      $this->global_models->get_connect("default");
//      $current = $this->get_current_sta($mahasiswa[0]->course_category);
      
      $pst = $this->input->post(NULL);
      if($pst['id_detail']){
        $kirim = array(
            "title"               => $pst['title'],
            "code"                => $pst['code'],
            "sort"                => $pst['sort'],
            "update_by_users"     => $this->session->userdata("id")
        );
        $id_keuangan_mahasiswa_jenis_biaya = $this->global_models->update("keuangan_mahasiswa_jenis_biaya", array("id_keuangan_mahasiswa_jenis_biaya" => $pst['id_detail']),$kirim);
      }
      else{
        $this->global_models->get_connect("zikula");
        $ao_applicant = $this->global_models->get("academicportal_ao_applicant", array("id" => $id_ao_applicant));
        if($ao_applicant[0]->lulus_pilihan_ke == 1){
          $course_category = $ao_applicant[0]->program_kuliah_pilihan_1;
        }
        else{
          $course_category = $ao_applicant[0]->program_kuliah_pilihan_2;
        }
        $fakultas = $this->global_models->get_field("academicportal_course_categories", "parent", array("id" => $course_category));
        $this->global_models->get_connect("default");
        
        $id_pembayaran_mahasiswa = $this->nbscache->get_explode("variable", "id_pembayaran_mahasiswa");
        $id_piutang_mahasiswa = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_fakultas_beban_mahasiswa_".$fakultas);
        $id_hutang_mahasiswa = $this->nbscache->get_explode("variable", "id_hutang_mahasiswa");
        
//        $total_debit = $this->global_models->get_query("
//          SELECT SUM(A.saldo) AS saldo
//          FROM keuangan_mahasiswa_tanggungan AS A
//          WHERE A.nim = '{$nim}' AND A.pos = 1
//          ");
//
//        $total_kredit = $this->global_models->get_query("
//          SELECT SUM(A.saldo) AS saldo
//          FROM keuangan_mahasiswa_tanggungan AS A
//          WHERE A.nim = '{$nim}' AND A.pos = 2
//          ");
          
        $total_tanggungan = $this->mfrm->get_tunggakan_mahasiswa_baru($id_ao_applicant);
//        $this->debug($total_debit);
//        $this->debug($total_kredit);
//        $this->debug($total_tanggungan, true);
        $kirim = array(
            "nim"                 => $pst['nim'],
            "id_keuangan_mahasiswa_jenis_virtual_account" => $id_keuangan_mahasiswa_jenis_virtual_account,
            "id_frm_account"      => $id_pembayaran_mahasiswa[1],
            "id_ao_applicant"     => $id_ao_applicant,
            "notransaksi"         => $pst['notransaksi'],
            "saldo"               => $this->global_models->string_to_number($pst['saldo']),
            "pos"                 => 2,
            "id_semester"         => $schedule[0]->semester,
            "tahun"               => $schedule[0]->tahun_ajaran,
            "note"                => $pst['note'],
            "create_by_users"     => $this->session->userdata("id"),
            "create_date"         => $pst['tanggal']
        );
        $id_keuangan_mahasiswa_baru_tanggungan = $this->global_models->insert("keuangan_mahasiswa_baru_tanggungan", $kirim);
        
        $kirim = array(
            "id_frm_journal"      => $this->mfrm->get_journal_current(),
            "notransaksi"         => $pst['notransaksi'],
            "saldo"               => $this->global_models->string_to_number($pst['saldo']),
            "tanggal"             => $pst['tanggal'],
            "note"                => "Pembayaran Biaya Tanggungan <a href='".site_url("keuangan-mahasiswa/kas-mahasiswa-baru/".$pst['nim'])."'>{$pst['nim']}</a> -- ".$pst['note'],
        );
            
        $id_frm_journal_log = $this->mfrm->create_journal_log($kirim);
        if($id_frm_journal_log){
          if($total_tanggungan <= 0){
            
            $debit[] = $this->global_models->string_to_number($pst['saldo']);
            $debit_da[] = $id_pembayaran_mahasiswa[1];

            $kredit[] = $this->global_models->string_to_number($pst['saldo']);
            $kredit_da[] = $id_piutang_mahasiswa[1];
//            $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);
            
          } 
          else{
            if(($total_tanggungan - $this->global_models->string_to_number($pst['saldo'])) >= 0){
              
              $debit[] = $this->global_models->string_to_number($pst['saldo']);
              $debit_da[] = $id_pembayaran_mahasiswa[1];

              $kredit[] = $this->global_models->string_to_number($pst['saldo']);
              $kredit_da[] = $id_piutang_mahasiswa[1];
//              $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);

            }
            else{
              
              $debit[] = $this->global_models->string_to_number($pst['saldo']);
              $debit_da[] = $id_pembayaran_mahasiswa[1];

              $kredit[] = abs($total_tanggungan);
              $kredit_da[] = $id_piutang_mahasiswa[1];
              $kredit[] = $this->global_models->string_to_number($pst['saldo']) - $total_tanggungan;
              $kredit_da[] = $id_hutang_mahasiswa[1];
              
              
            }
          }
          $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);
        }
        
//        cek lunas
        $total_debit = $this->global_models->get_query("
          SELECT SUM(A.saldo) AS saldo
          FROM keuangan_mahasiswa_baru_tanggungan AS A
          WHERE A.id_ao_applicant = '{$id_ao_applicant}' AND A.pos = 1
          ");

        $total_kredit = $this->global_models->get_query("
          SELECT SUM(A.saldo) AS saldo
          FROM keuangan_mahasiswa_baru_tanggungan AS A
          WHERE A.id_ao_applicant = '{$id_ao_applicant}' AND (A.pos = 2 OR A.pos = 3)
          ");
          
//        $pos_asli = $this->global_models->get_field("frm_account", "pos", array("id_frm_account" => $id_frm_account));
//        if($pos_asli == 1)
//          $total_akhir = $total_debit[0]->saldo - $total_kredit[0]->saldo;
//        else
        $total_akhir = $total_kredit[0]->saldo - $total_debit[0]->saldo;  
        
        if($total_akhir >= 0){
//          set keuangan
          $this->global_models->get_connect("zikula");
          $this->global_models->update("academicportal_ao_applicant", array("id" => $mahasiswa[0]->id),
                  array("status_uang_pangkal" => 2));
          $this->global_models->get_connect("default");
        }
          
      }
      if($id_keuangan_mahasiswa_baru_tanggungan){
        $this->session->set_flashdata('success', 'Data tersimpan');
      }
      else{
        $this->session->set_flashdata('notice', 'Data tidak tersimpan');
      }
      redirect("keuangan-mahasiswa/kas-jenis-biaya-detail-baru/{$id_keuangan_mahasiswa_jenis_virtual_account}/".$id_ao_applicant);
    }
    else{
      $detail = $this->global_models->get("keuangan_mahasiswa_baru_tanggungan", array("id_keuangan_mahasiswa_baru_tanggungan" => $id_keuangan_mahasiswa_bru_tanggungan));
      $this->template->build("add-payment-mahasiswa-baru",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'keuangan-mahasiswa/mahasiswa-baru',
              'title'       => lang("keuangan_mahasiswa_baru_payment"),
              'detail'      => $detail,
              'id_ao_applicant'         => $id_ao_applicant,
              'id_keuangan_mahasiswa_jenis_virtual_account' => $id_keuangan_mahasiswa_jenis_virtual_account
            ));
      $this->template
        ->set_layout('default')
        ->build("add-payment-mahasiswa-baru");
    }
	}
  
  public function add_tanggungan($id_keuangan_mahasiswa_jenis_virtual_account, $nim, $id_keuangan_mahasiswa_tanggungan = 0){
    if($this->input->post(NULL)){
      $this->global_models->get_connect("zikula");
      
      $mahasiswa = $this->global_models->get_query("
        SELECT A.*
        FROM academicportal_student_categories AS A
        LEFT JOIN academicportal_mahasiswa AS B ON A.users = B.pn_uid
        WHERE B.nim = '{$nim}'");
//      $current = $this->global_models->get("academicportal_semester_tahun_ajaran_berjalan", array("is_running" => 1));
      $fakultas = $this->global_models->get_field("academicportal_course_categories", "parent", array("id" => $mahasiswa[0]->course_category));
      $this->global_models->get_connect("default");
      $pst = $this->input->post(NULL);
      if($pst['id_detail']){
        $kirim = array(
            "title"               => $pst['title'],
            "code"                => $pst['code'],
            "sort"                => $pst['sort'],
            "update_by_users"     => $this->session->userdata("id")
        );
        $id_keuangan_mahasiswa_jenis_biaya = $this->global_models->update("keuangan_mahasiswa_jenis_biaya", array("id_keuangan_mahasiswa_jenis_biaya" => $pst['id_detail']),$kirim);
      }
      else{
//        $id_pembayaran_mahasiswa = $this->nbscache->get_explode("variable", "id_pembayaran_mahasiswa");
//        $id_piutang_mahasiswa = $this->nbscache->get_explode("variable", "id_piutang_mahasiswa");
//        $id_hutang_mahasiswa = $this->nbscache->get_explode("variable", "id_hutang_mahasiswa");
        
//        $id_pembayaran_mahasiswa = $this->nbscache->get_explode("variable", "id_pembayaran_mahasiswa");
        $id_piutang_mahasiswa = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_fakultas_beban_mahasiswa_".$fakultas);
        $id_pembalik_kelebihan_bayar = $this->nbscache->get_explode("variable", "id_pembalik_kelebihan_bayar");
        
        $total_debit = $this->global_models->get_query("
          SELECT SUM(A.saldo) AS saldo
          FROM keuangan_mahasiswa_tanggungan AS A
          WHERE A.nim = '{$nim}' AND (A.pos = 1 OR A.pos = 3)
          ");

        $total_kredit = $this->global_models->get_query("
          SELECT SUM(A.saldo) AS saldo
          FROM keuangan_mahasiswa_tanggungan AS A
          WHERE A.nim = '{$nim}' AND A.pos = 2
          ");
          
        $total_tanggungan = $this->mfrm->get_tunggakan_komponen($nim, $id_keuangan_mahasiswa_jenis_virtual_account);
//        $this->debug($total_debit); 
//        $this->debug($total_kredit);
//        $this->debug($total_tanggungan, true);
        $this->global_models->get_connect("zikula");
        $program_kuliah = $this->global_models->get("academicportal_mahasiswa", array("nim" => $pst['nim']));
        $this->global_models->get_connect("default");
        
        $current = $this->get_current_sta_prodi($program_kuliah);
        
        $kirim_tanggungan = array(
            "nim"                 => $pst['nim'],
            "notransaksi"         => $pst['notransaksi'],
            "saldo"               => $this->global_models->string_to_number($pst['saldo']),
            "pos"                 => 1,
            "note"                => $pst['note'],
            "id_program_kuliah"   => $program_kuliah[0]->program_kuliah,
            "id_kelompok"         => $program_kuliah[0]->kelompok_mahasiswa,
            "id_keuangan_mahasiswa_jenis_virtual_account"   => $id_keuangan_mahasiswa_jenis_virtual_account,
            "id_frm_account"                                => $pst['id_frm_account'],
            "tahunsemester"       => $pst['tahunsemster'],
            "create_by_users"                               => $this->session->userdata("id"),
            "create_date"         => $pst['tanggal']
        );

        $id_keuangan_mahasiswa_tanggungan = $this->global_models->insert("keuangan_mahasiswa_tanggungan", $kirim_tanggungan);
        
        $kirim = array(
            "id_frm_journal"      => $this->mfrm->get_journal_current(),
            "notransaksi"         => $pst['notransaksi'],
            "saldo"               => $this->global_models->string_to_number($pst['saldo']),
            "tanggal"             => $pst['tanggal'],
            "note"                => "Biaya Tanggungan <a href='".site_url("keuangan-mahasiswa/kas-jenis-biaya-detail/{$id_keuangan_mahasiswa_jenis_virtual_account}/".$pst['nim'])."'>{$pst['nim']}</a> -- ".$pst['note'],
        );
            
        $id_frm_journal_log = $this->mfrm->create_journal_log($kirim);
        if($id_frm_journal_log){
          if($total_tanggungan >= 0){
            
            $debit[] = $this->global_models->string_to_number($pst['saldo']);
            $debit_da[] = $id_piutang_mahasiswa[1];

            $kredit[] = $this->global_models->string_to_number($pst['saldo']);
            $kredit_da[] = $pst['id_frm_account'];
            $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);
            
          } 
          else{
            if(($total_tanggungan + $this->global_models->string_to_number($pst['saldo'])) > 0){
              
              $debit[] = ($total_tanggungan + $this->global_models->string_to_number($pst['saldo']));
              $debit_da[] = $id_piutang_mahasiswa[1];

              $debit[] = abs($total_tanggungan);
              $debit_da[] = $id_pembalik_kelebihan_bayar[1];;
              
              $kredit[] = $this->global_models->string_to_number($pst['saldo']);
              $kredit_da[] = $pst['id_frm_account'];
              $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);
              
            }
            else{
              
              $debit[] = $this->global_models->string_to_number($pst['saldo']);
              $debit_da[] = $id_pembalik_kelebihan_bayar[1];

              $kredit[] = $this->global_models->string_to_number($pst['saldo']);
              $kredit_da[] = $pst['id_frm_account'];
              $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);
              
            }
          }
        }
      }
      if($id_keuangan_mahasiswa_tanggungan){
        $this->session->set_flashdata('success', 'Data tersimpan');
      }
      else{
        $this->session->set_flashdata('notice', 'Data tidak tersimpan');
      }
      redirect("keuangan-mahasiswa/kas-jenis-biaya-detail/{$id_keuangan_mahasiswa_jenis_virtual_account}/".$pst['nim']);
    }
    else{
      $detail = $this->global_models->get("keuangan_mahasiswa_tanggungan", array("id_keuangan_mahasiswa_tanggungan" => $id_keuangan_mahasiswa_tanggungan));
      
      $this->global_models->get_connect("zikula");
      $program_kuliah = $this->global_models->get_field("academicportal_mahasiswa", "program_kuliah", array("nim" => $nim));
      $sta_dropdown_temp = $this->global_models->get_query("
        SELECT A. semester, A.tahun_ajaran, B.semester_name
        FROM academicportal_semester_tahun_ajaran AS A
        LEFT JOIN academicportal_semester AS B ON A.semester = B.id
        WHERE program_kuliah = {$program_kuliah}");
      $this->global_models->get_connect("default");
      
      foreach($sta_dropdown_temp AS $sdt){
        $sta_dropdown[$sdt->tahun_ajaran.$sdt->semester] = $sdt->semester_name." - ".$sdt->tahun_ajaran."/".($sdt->tahun_ajaran + 1);
      }
      
      $this->template->build("add-tanggungan-mahasiswa",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'keuangan-mahasiswa/mahasiswa',
              'title'       => lang("keuangan_mahasiswa_tambahan_tanggungan"),
              'detail'      => $detail,
              'nim'         => $nim,
              'sta_dropdown' => $sta_dropdown,
            ));
      $this->template
        ->set_layout('default')
        ->build("add-tanggungan-mahasiswa");
    }
	}
  
  public function add_tanggungan_mahasiswa_baru($nim, $id_keuangan_mahasiswa_baru_tanggungan = 0){
    if($this->input->post(NULL)){
      $this->global_models->get_connect("zikula");
//      $current = $this->global_models->get("academicportal_semester_tahun_ajaran_berjalan", array("is_running" => 1));
      $mahasiswa = $this->global_models->get_query("
        SELECT A.*
        FROM academicportal_student_categories AS A
        LEFT JOIN academicportal_mahasiswa AS B ON A.users = B.pn_uid
        WHERE B.nim = '{$nim}'");
//      $current = $this->global_models->get("academicportal_semester_tahun_ajaran_berjalan", array("is_running" => 1));
      $fakultas = $this->global_models->get_field("academicportal_course_categories", "parent", array("id" => $mahasiswa[0]->course_category));
      $this->global_models->get_connect("default");
      $pst = $this->input->post(NULL);
      if($pst['id_detail']){
        $kirim = array(
            "title"               => $pst['title'],
            "code"                => $pst['code'],
            "sort"                => $pst['sort'],
            "update_by_users"     => $this->session->userdata("id")
        );
        $id_keuangan_mahasiswa_jenis_biaya = $this->global_models->update("keuangan_mahasiswa_jenis_biaya", array("id_keuangan_mahasiswa_jenis_biaya" => $pst['id_detail']),$kirim);
      }
      else{
//        $id_pembayaran_mahasiswa = $this->nbscache->get_explode("variable", "id_pembayaran_mahasiswa");
//        $id_piutang_mahasiswa = $this->nbscache->get_explode("variable", "id_piutang_mahasiswa");
//        $id_hutang_mahasiswa = $this->nbscache->get_explode("variable", "id_hutang_mahasiswa");
        
//        $id_pembayaran_mahasiswa = $this->nbscache->get_explode("variable", "id_pembayaran_mahasiswa");
        $id_piutang_mahasiswa = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_fakultas_beban_mahasiswa_".$fakultas);
        $id_pembalik_kelebihan_bayar = $this->nbscache->get_explode("variable", "id_pembalik_kelebihan_bayar");
        
        $total_debit = $this->global_models->get_query("
          SELECT SUM(A.saldo) AS saldo
          FROM keuangan_mahasiswa_tanggungan AS A
          WHERE A.nim = '{$nim}' AND A.pos = 1
          ");

        $total_kredit = $this->global_models->get_query("
          SELECT SUM(A.saldo) AS saldo
          FROM keuangan_mahasiswa_tanggungan AS A
          WHERE A.nim = '{$nim}' AND A.pos = 2
          ");
          
        $total_tanggungan = $this->mfrm->get_tunggakan($nim);
//        $this->debug($total_debit);
//        $this->debug($total_kredit);
        $this->debug($total_tanggungan, true);
        $this->global_models->get_connect("zikula");
        $program_kuliah = $this->global_models->get("academicportal_mahasiswa", array("nim" => $pst['nim']));
        $this->global_models->get_connect("default");
        
        $current = $this->get_current_sta_prodi($program_kuliah);
        
        $kirim = array(
            "nim"                 => $pst['nim'],
            "id_program_kuliah"   => $program_kuliah[0]->program_kuliah,
            "id_kelompok"         => $program_kuliah[0]->kelompok_mahasiswa,
            "id_frm_account"      => $id_pembayaran_mahasiswa[1],
            "notransaksi"         => $pst['notransaksi'],
            "saldo"               => $this->global_models->string_to_number($pst['saldo']),
            "pos"                 => 1,
            "id_semester"         => $current[0]->id,
            "tahun"               => $current[0]->tahun_ajaran,
            "tahunsemester"       => $current[0]->tahun_ajaran.$current[0]->id,
            "note"                => $pst['note'],
            "create_by_users"     => $this->session->userdata("id"),
            "create_date"         => $pst['tanggal']
        );
        $id_keuangan_mahasiswa_tanggungan = $this->global_models->insert("keuangan_mahasiswa_tanggungan", $kirim);
        
        $kirim = array(
            "id_frm_journal"      => $this->mfrm->get_journal_current(),
            "notransaksi"         => $pst['notransaksi'],
            "saldo"               => $this->global_models->string_to_number($pst['saldo']),
            "tanggal"             => $pst['tanggal'],
            "note"                => "Pembayaran Biaya Tanggungan <a href='".site_url("keuangan-mahasiswa/kas-mahasiswa/".$pst['nim'])."'>{$pst['nim']}</a> -- ".$pst['note'],
        );
            
        $id_frm_journal_log = $this->mfrm->create_journal_log($kirim);
        if($id_frm_journal_log){
          if($total_tanggungan > 0){
            
            $debit[] = $this->global_models->string_to_number($pst['saldo']);
            $debit_da[] = $id_piutang_mahasiswa[1];

            $kredit[] = $this->global_models->string_to_number($pst['saldo']);
            $kredit_da[] = $pst['id_frm_account'];
            $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);
            
          } 
          else{
            if(($total_tanggungan + $this->global_models->string_to_number($pst['saldo'])) <= 0){
              
              $debit[] = $this->global_models->string_to_number($pst['saldo']);
              $debit_da[] = $id_pembalik_kelebihan_bayar[1];

              $kredit[] = $this->global_models->string_to_number($pst['saldo']);
              $kredit_da[] = $pst['id_frm_account'];
              $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);

            }
            else{
              
              $debit[] = ($total_tanggungan + $this->global_models->string_to_number($pst['saldo']));
              $debit_da[] = $id_piutang_mahasiswa[1];

              $kredit[] = abs($total_tanggungan);
              $kredit_da[] = $id_pembalik_kelebihan_bayar[1];;
              
              $kredit[] = $this->global_models->string_to_number($pst['saldo']);
              $kredit_da[] = $pst['id_frm_account'];
              $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);
              
            }
          }
        }
      }
      if($id_keuangan_mahasiswa_tanggungan){
        $this->session->set_flashdata('success', 'Data tersimpan');
      }
      else{
        $this->session->set_flashdata('notice', 'Data tidak tersimpan');
      }
      redirect("keuangan-mahasiswa/kas-mahasiswa-baru/".$pst['nim']);
    }
    else{
      $detail = $this->global_models->get("keuangan_mahasiswa_baru_tanggungan", array("id_keuangan_mahasiswa_baru_tanggungan" => $id_keuangan_mahasiswa_baru_tanggungan));
      $this->template->build("add-tanggungan-mahasiswa-baru",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'keuangan-mahasiswa/mahasiswa-baru',
              'title'       => lang("keuangan_mahasiswa_payment"),
              'detail'      => $detail,
              'nim'         => $nim
            ));
      $this->template
        ->set_layout('default')
        ->build("add-tanggungan-mahasiswa-baru");
    }
	}
  
  public function setting_account_mahasiswa(){
    if($this->input->post(NULL)){
      $pst = $this->input->post(NULL);
      foreach($pst AS $key => $value){
        if(substr($key, 0, 2) == "id"){
          $this->nbscache->put_tunggal("variable", $key, $value);
        }
      }
      
    }
//    else{
    $this->template->build("setting-account-mahasiswa",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-mahasiswa/setting-account-mahasiswa',
            'title'       => lang("keuangan_mahasiswa_setting_account_transaksi"),
          ));
    $this->template
      ->set_layout('default')
      ->build("setting-account-mahasiswa");
//    }
	}
  
  public function setting_general(){
    if($this->input->post(NULL)){
      $pst = $this->input->post(NULL);
//      sks
      $this->nbscache->put_tunggal("variable", "keuangan_mahasiswa_jenis_biaya_sks", $pst['keuangan_mahasiswa_jenis_biaya_sks']);
      $this->nbscache->put_tunggal("variable", "keuangan_mahasiswa_jenis_biaya_sks_masuk", $pst['keuangan_mahasiswa_jenis_biaya_sks_masuk']);
      $this->nbscache->put_tunggal("variable", "keuangan_mahasiswa_pendapatan_her_registrasi", $pst['keuangan_mahasiswa_pendapatan_her_registrasi']);
      $this->nbscache->put_tunggal("variable", "keuangan_mahasiswa_vid_btn", $pst['keuangan_mahasiswa_vid_btn']);
      $this->nbscache->put_tunggal("variable", "keuangan_mahasiswa_kode_institusi", $pst['keuangan_mahasiswa_kode_institusi']);
      $this->nbscache->put_tunggal("variable", "keuangan_mahasiswa_virtual_account_her_registrasi", $pst['keuangan_mahasiswa_virtual_account_her_registrasi']);
      $this->nbscache->put_tunggal("variable", "keuangan_mahasiswa_virtual_account_biaya_kuliah", $pst['keuangan_mahasiswa_virtual_account_biaya_kuliah']);
      $this->nbscache->put_tunggal("variable", "keuangan_mahasiswa_formulir_pendaftaran", $pst['keuangan_mahasiswa_formulir_pendaftaran']);
//      sks
    }
//    else{
    $this->template->build("setting",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-mahasiswa/setting-general',
            'title'       => lang("settings"),
            'virtual'     => $this->global_models->get_dropdown("keuangan_mahasiswa_jenis_virtual_account", "id_keuangan_mahasiswa_jenis_virtual_account", "title"),
          ));
    $this->template
      ->set_layout('default')
      ->build("setting");
//    }
	}
  
  public function mahasiswa($cari){
    
    if($this->input->post()){
      $pst = $this->input->post();
//      $this->debug($pst, true);
      if($pst['cari'] == 'cari'){
        $this->session->set_userdata(array(
            "keuangan_mahasiswa_cari_mahasiswa" => $pst['cari_name_nim'],
            "keuangan_mahasiswa_cari_status"  => $pst['cari_status'],
            "keuangan_mahasiswa_cari_course_category" => $pst['cari_course_category'],
            "keuangan_mahasiswa_cari_kelompok" => $pst['cari_kelompok']
            ));
      }
      else{
        $this->session->set_userdata(array(
            "keuangan_mahasiswa_cari_mahasiswa" => "",
            "keuangan_mahasiswa_cari_course_category" => "",
            "keuangan_mahasiswa_cari_status" => "",
            "keuangan_mahasiswa_cari_kelompok" => ""
            ));
      }
    }
    
    $query = "";
    if($this->session->userdata("keuangan_mahasiswa_cari_mahasiswa")){
      $query .= "AND (LOWER(B.nim) LIKE '%".strtolower($this->session->userdata("keuangan_mahasiswa_cari_mahasiswa"))."%' OR LOWER(B.nama) LIKE '%".strtolower($this->session->userdata("keuangan_mahasiswa_cari_mahasiswa"))."%')";
    }
    if($this->session->userdata("keuangan_mahasiswa_cari_course_category") > 0){
      $query .= " AND A.program_kuliah = {$this->session->userdata("keuangan_mahasiswa_cari_course_category")}";
    }
    if($this->session->userdata("keuangan_mahasiswa_cari_status")){
      $query .= " AND A.student_status = '{$this->session->userdata("keuangan_mahasiswa_cari_status")}'";
    }
    if($this->session->userdata("keuangan_mahasiswa_cari_kelompok")){
      $query .= " AND A.kelompok_mahasiswa = '{$this->session->userdata("keuangan_mahasiswa_cari_kelompok")}'";
    }
    
    $this->global_models->get_connect("zikula");
//    FROM academicportal_student_categories AS A
//        LEFT JOIN academicportal_mahasiswa AS B
    $total = $this->global_models->get_query("
      SELECT count(B.nim) AS jumlah
      FROM academicportal_student_categories AS A
      LEFT JOIN academicportal_mahasiswa AS B ON A.users = B.pn_uid
      WHERE B.nim IS NOT NULL 
      $query
      ");
    
    $course_category = $this->global_models->get_dropdown("academicportal_course_categories", "id", "name", true, array("set_as_program_jurusan" => 1));
    $status_mahasiswa = array(
        ""    => '- Pilih -',
        "A"   => 'AKTIF',
        "C"   => 'CUTI',
        "N"   => 'DICUTIKAN',
        "K"   => 'KELUAR',
        "L"   => 'LULUS',
        "D"   => 'DROP-OUT'
    );
    
    $this->global_models->get_connect("zikula");
    
    $prokul = $this->global_models->get_query("
      SELECT A.id_program_kuliah, B.name, A.nama_program
      FROM academicportal_program_kuliah AS A
      LEFT JOIN academicportal_course_categories AS B ON A.category = B.id
      ");
    
    $kelompok_drop = $this->global_models->get_dropdown("academicportal_kelompok_mahasiswa", "id_kelompok_mahasiswa", "nama_kelompok", TRUE);
    
    $this->global_models->get_connect("default");
    
    $prokul_drop[] = '- Pilih -';
    foreach($prokul AS $pk){
      $prokul_drop[$pk->id_program_kuliah] = $pk->name." - ".$pk->nama_program;
    }
    
    $this->global_models->get_connect("default");  
    $this->template->build("mahasiswa",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-mahasiswa/mahasiswa',
            'title'       => lang("keuangan_mahasiswa_mahasiswa"),
            'total'       => $total[0]->jumlah,
            'course_category' => $prokul_drop,
            'status_mahasiswa' => $status_mahasiswa,
            'kelompok'    => $kelompok_drop
          ));
    $this->template
      ->set_layout('default')
      ->build("mahasiswa");
	}
  
  public function mahasiswa_baru($cari = ""){
    if($cari == "nubusa_clear_search"){
      $this->session->set_userdata(array("keuangan_mahasiswa_cari_mahasiswa_baru" => ""));
      $cari = "";
    }
    else if(!$cari){
      $cari = $this->session->userdata("keuangan_mahasiswa_cari_mahasiswa_baru");
    }
    else{
      $this->session->set_userdata(array("keuangan_mahasiswa_cari_mahasiswa_baru" => $cari));
    }
    
    $this->global_models->get_connect("zikula");
//    FROM academicportal_student_categories AS A
//        LEFT JOIN academicportal_mahasiswa AS B
    $query = "
      SELECT count(A.id) AS jumlah
      FROM academicportal_ao_applicant AS A
      WHERE A.id NOT IN (SELECT ao_applicant_id FROM academicportal_mahasiswa WHERE ao_applicant_id IS NOT NULL AND ao_applicant_id > 0)
      
      ";
    if($cari){
      $query .= "AND (LOWER(fullname) LIKE '%".strtolower($cari)."%' OR LOWER(ref_number) LIKE '%".strtolower($cari)."%')";
    }
    
    $total = $this->global_models->get_query($query);
    $this->global_models->get_connect("default");  
    $this->template->build("mahasiswa-baru",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-mahasiswa/mahasiswa-baru',
            'title'       => lang("keuangan_mahasiswa_mahasiswa_baru"),
            'total'       => $total[0]->jumlah
          ));
    $this->template
      ->set_layout('default')
      ->build("mahasiswa-baru");
	}
  
  public function kas_mahasiswa($nim){
    
    $total = $this->global_models->get_query("
      SELECT count(A.id_keuangan_mahasiswa_tanggungan) AS jumlah
      FROM keuangan_mahasiswa_tanggungan AS A WHERE A.nim = '{$nim}'
      ");
    $this->template->build("kas",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-mahasiswa/mahasiswa',
            'title'       => lang("keuangan_mahasiswa_kas"),
            'total'       => $total[0]->jumlah,
            'nim'         => $nim,
            'id_keuangan_mahasiswa_jenis_virtual_account' => 0
          ));
    $this->template
      ->set_layout('default')
      ->build("kas");
	}
  
  public function kas_jenis_biaya_detail($id_keuangan_mahasiswa_jenis_virtual_account, $nim){
    
    $total = $this->global_models->get_query("
      SELECT count(A.id_keuangan_mahasiswa_tanggungan) AS jumlah
      FROM keuangan_mahasiswa_tanggungan AS A WHERE A.nim = '{$nim}'
        AND A.id_keuangan_mahasiswa_jenis_virtual_account = {$id_keuangan_mahasiswa_jenis_virtual_account}
      ");
    $this->template->build("kas",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-mahasiswa/mahasiswa',
            'title'       => lang("keuangan_mahasiswa_kas"),
            'total'       => $total[0]->jumlah,
            'nim'         => $nim,
            'id_keuangan_mahasiswa_jenis_virtual_account' => $id_keuangan_mahasiswa_jenis_virtual_account
          ));
    $this->template
      ->set_layout('default')
      ->build("kas");
	}
  
  public function kas_jenis_biaya_detail_baru($id_keuangan_mahasiswa_jenis_virtual_account, $id_ao_applicant){
    
    $total = $this->global_models->get_query("
      SELECT count(A.id_keuangan_mahasiswa_baru_tanggungan) AS jumlah
      FROM keuangan_mahasiswa_baru_tanggungan AS A WHERE A.id_ao_applicant = '{$id_ao_applicant}'
        AND A.id_keuangan_mahasiswa_jenis_virtual_account = {$id_keuangan_mahasiswa_jenis_virtual_account}
      ");
    $this->template->build("kas-baru",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-mahasiswa/mahasiswa-baru',
            'title'       => lang("keuangan_mahasiswa_baru_kas"),
            'total'       => $total[0]->jumlah,
            'id_ao_applicant'         => $id_ao_applicant,
            'id_keuangan_mahasiswa_jenis_virtual_account' => $id_keuangan_mahasiswa_jenis_virtual_account
          ));
    $this->template
      ->set_layout('default')
      ->build("kas-baru");
	}
  
  public function kas_mahasiswa_baru($id_ao_applicant){
    
    $total = $this->global_models->get_query("
      SELECT count(A.id_keuangan_mahasiswa_baru_tanggungan) AS jumlah
      FROM keuangan_mahasiswa_baru_tanggungan AS A WHERE A.id_ao_applicant = '{$id_ao_applicant}'
      ");
    $this->template->build("kas-baru",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-mahasiswa/mahasiswa-baru',
            'title'       => lang("keuangan_mahasiswa_baru_kas"),
            'total'       => $total[0]->jumlah,
            'id_ao_applicant'         => $id_ao_applicant
          ));
    $this->template
      ->set_layout('default')
      ->build("kas-baru");
	}
  
  public function ajax_mahasiswa($total = 0, $start = 0){
    $this->global_models->get_connect("zikula");
//    FROM academicportal_student_categories AS A
//        LEFT JOIN academicportal_mahasiswa AS B
    
    $query = "";
//    if($this->session->userdata("keuangan_mahasiswa_cari_mahasiswa")){
//      $query = "AND (LOWER(A.nim) LIKE '%".strtolower($this->session->userdata("keuangan_mahasiswa_cari_mahasiswa"))."%' OR LOWER(A.nama) LIKE '%".strtolower($this->session->userdata("keuangan_mahasiswa_cari_mahasiswa"))."%')";
//    }
    
    if($this->session->userdata("keuangan_mahasiswa_cari_mahasiswa")){
      $query .= "AND (LOWER(A.nim) LIKE '%".strtolower($this->session->userdata("keuangan_mahasiswa_cari_mahasiswa"))."%' OR LOWER(A.nama) LIKE '%".strtolower($this->session->userdata("keuangan_mahasiswa_cari_mahasiswa"))."%')";
    }
    if($this->session->userdata("keuangan_mahasiswa_cari_course_category") > 0){
      $query .= " AND C.program_kuliah = {$this->session->userdata("keuangan_mahasiswa_cari_course_category")}";
    }
    if($this->session->userdata("keuangan_mahasiswa_cari_status")){
      $query .= " AND C.student_status = '{$this->session->userdata("keuangan_mahasiswa_cari_status")}'";
    }
    if($this->session->userdata("keuangan_mahasiswa_cari_kelompok")){
      $query .= " AND C.kelompok_mahasiswa = '{$this->session->userdata("keuangan_mahasiswa_cari_kelompok")}'";
    }
    
    $data = $this->global_models->get_query("
      SELECT A.*, B.deskripsi AS jenjang, C.student_status, D.nama_kelompok
      FROM academicportal_student_categories AS C
      LEFT JOIN academicportal_mahasiswa AS A ON C.users = A.pn_uid
      LEFT JOIN academicportal_kd_jenjang_strata_program_studi  AS B ON A.kode_jenjang_studi = B.kode
      LEFT JOIN academicportal_kelompok_mahasiswa AS D ON A.kelompok_mahasiswa = D.id_kelompok_mahasiswa
      WHERE A.nim IS NOT NULL
      $query
      ORDER BY A.nim LIMIT {$start},10
      ");
    $this->global_models->get_connect("default");  
    $status_mahasiswa = array(
        "A"   => 'AKTIF',
        "C"   => 'CUTI',
        "N"   => 'DICUTIKAN',
        "K"   => 'KELUAR',
        "L"   => 'LULUS',
        "D"   => 'DROP-OUT'
    );
    foreach ($data as $key => $value) {
      $beban = $this->global_models->get_query("
        SELECT sum(saldo) AS sld
        FROM keuangan_mahasiswa_tanggungan
        WHERE nim = '{$value->nim}' AND id_keuangan_mahasiswa_jenis_virtual_account > 0
        AND pos = 1
        ");
      $beban2 = $this->global_models->get_query("
        SELECT sum(saldo) AS sld
        FROM keuangan_mahasiswa_tanggungan
        WHERE nim = '{$value->nim}' AND id_keuangan_mahasiswa_jenis_virtual_account > 0
        AND pos = 2
        ");
      $beban3 = $this->global_models->get_query("
        SELECT sum(saldo) AS sld
        FROM keuangan_mahasiswa_tanggungan
        WHERE nim = '{$value->nim}' AND id_keuangan_mahasiswa_jenis_virtual_account > 0
        AND pos = 3
        ");
//      $this->debug($beban, true);
      print "
      <tr class='ajax-quotation'>
        <td>{$value->nim}</td>
        <td>{$value->nama}</td>
        <td>{$value->nama_kelompok}</td>
        <td>{$status_mahasiswa[$value->student_status]}</td>
        <td style='text-align: right'>".  number_format($beban[0]->sld, 0, ",", ".")."</td>
        <td style='text-align: right'>".  number_format($beban2[0]->sld, 0, ",", ".")."</td>
        <td style='text-align: right'>".  number_format($beban3[0]->sld, 0, ",", ".")."</td>
        <td style='text-align: right'>".  number_format(($beban[0]->sld - $beban2[0]->sld + $beban3[0]->sld), 0, ",", ".")."</td>
        <td>
          <div class='btn-group'>
            <button data-toggle='dropdown' class='btn btn-small dropdown-toggle'>Action<span class='caret'></span></button>
            <ul class='dropdown-menu'>
              <li><a href='".site_url("keuangan-mahasiswa/kas-mahasiswa-detail/".$value->nim)."'>Kas Detail</a></li>
              <li><a href='".site_url("keuangan-mahasiswa/pass-status-keuangan-mahasiswa/".$value->nim)."'>Pass Status Keuangan</a></li>
            </ul>
          </div>
        </td>
      </tr>";
        
    }
    
    $this->load->library('pagination');

    $config['base_url'] = '';
    $config['total_rows'] = $total;
    $config['per_page'] = 10; 
    $config['uri_segment'] = 4; 
    $config['cur_tag_open'] = "<li class='active'><a href='javascript:void(0)'>"; 
    $config['cur_tag_close'] = "</a></li>"; 
    $config['first_tag_open'] = "<li>"; 
    $config['first_tag_close'] = "</li>"; 
    $config['last_tag_open'] = "<li>"; 
    $config['last_tag_close'] = "</li>"; 
    $config['next_tag_open'] = "<li>"; 
    $config['next_tag_close'] = "</li>"; 
    $config['prev_tag_open'] = "<li>"; 
    $config['prev_tag_close'] = "</li>"; 
    $config['num_tag_open'] = "<li>"; 
    $config['num_tag_close'] = "</li>";
    $config['function_js'] = "load_quotation";

    $this->pagination->initialize($config); 
    print "
    <tr class='ajax-quotation'>
      <td colspan='9'>
        <div class='pagination'>
          <ul>
          ".$this->pagination->create_links_ajax()."
          </ul>
        </div>
      </td>
    </tr>";
    die;
  }
  
  public function ajax_mahasiswa_baru($total = 0, $start = 0){
    $this->global_models->get_connect("zikula");
//    FROM academicportal_student_categories AS A
//        LEFT JOIN academicportal_mahasiswa AS B
    
    if($this->session->userdata("keuangan_mahasiswa_cari_mahasiswa_baru")){
      $query = "AND (LOWER(A.fullname) LIKE '%".strtolower($this->session->userdata("keuangan_mahasiswa_cari_mahasiswa_baru"))."%' OR LOWER(A.ref_number) LIKE '%".strtolower($this->session->userdata("keuangan_mahasiswa_cari_mahasiswa_baru"))."%')";
    }
    else{
      $query = "";
    }
    
    $data = $this->global_models->get_query("
      SELECT A.*, B.nama_test
      FROM academicportal_ao_applicant AS A
      LEFT JOIN academicportal_ao_jenis_test AS B ON B.id = A.jenis_test
      WHERE A.id NOT IN (SELECT ao_applicant_id FROM academicportal_mahasiswa WHERE ao_applicant_id IS NOT NULL AND ao_applicant_id > 0)
      {$query}
      ORDER BY A.ref_number LIMIT {$start},10
      ");
    $this->global_models->get_connect("default");  
    $submit = array(
        0 => "Delay",
        1 => "Ok",
    );
    $biaya_formulir = array(
        0 => "Belum Lunas",
        1 => "Lunas",
    );
    $ujian = array(
        0 => "Belum Ok",
        1 => "Ok",
    );
    $lulus = array(
        0 => " - ",
        1 => "Lulus Pilihan 1",
        2 => "Lulus Pilihan 2",
    );
    $registrasi = array(
        0 => "Belum Terdaftar",
        1 => "Terdaftar",
    );
    $uang_pangkal = array(
        1 => "Belum Lunas",
        2 => "Lunas",
    );
    foreach ($data as $key => $value) {
      if($value->status_uang_pangkal >= 1){
        $kas = "
          <li><a href='".site_url("keuangan-mahasiswa/kas-mahasiswa-baru-detail/".$value->id)."'>Kas Detail</a></li>
          <li><a href='".site_url("keuangan-mahasiswa/pass-status-keuangan-mahasiswa-baru/".$value->id)."'>Pass Status Keuangan</a></li>
          ";
      }
      elseif($value->status_pembayaran_formulir <= 0){
        $kas = "
          <li><a href='".site_url("keuangan-mahasiswa/bayar-biaya-formulir-mahasiswa-baru/".$value->id)."'>Bayar Biaya Formulir</a></li>
          ";
      }
      else{
        $kas = "";
      }
      print "
      <tr class='ajax-quotation'>
        <td>{$value->ref_number}</td>
        <td>{$value->fullname}</td>
        <td>{$value->nama_test}</td>
        <td>{$submit[$value->submit_status]}</td>
        <td>{$biaya_formulir[$value->status_pembayaran_formulir]}</td>
        <td>{$ujian[$value->status_data_pra_ujian]}</td>
        <td>{$lulus[$value->lulus_pilihan_ke]}</td>
        <td>{$uang_pangkal[$value->status_uang_pangkal]}</td>
        <td>{$registrasi[$value->status_registrasi_mahasiswa]}</td>
        <td>
          <div class='btn-group'>
            <button data-toggle='dropdown' class='btn btn-small dropdown-toggle'>Action<span class='caret'></span></button>
            <ul class='dropdown-menu'>
              $kas
            </ul>
          </div>
        </td>
      </tr>";
        
    }
    
    $this->load->library('pagination');

    $config['base_url'] = '';
    $config['total_rows'] = $total;
    $config['per_page'] = 10; 
    $config['uri_segment'] = 4; 
    $config['cur_tag_open'] = "<li class='active'><a href='javascript:void(0)'>"; 
    $config['cur_tag_close'] = "</a></li>"; 
    $config['first_tag_open'] = "<li>"; 
    $config['first_tag_close'] = "</li>"; 
    $config['last_tag_open'] = "<li>"; 
    $config['last_tag_close'] = "</li>"; 
    $config['next_tag_open'] = "<li>"; 
    $config['next_tag_close'] = "</li>"; 
    $config['prev_tag_open'] = "<li>"; 
    $config['prev_tag_close'] = "</li>"; 
    $config['num_tag_open'] = "<li>"; 
    $config['num_tag_close'] = "</li>";
    $config['function_js'] = "load_quotation";

    $this->pagination->initialize($config); 
    print "
    <tr class='ajax-quotation'>
      <td colspan='10'>
        <div class='pagination'>
          <ul>
          ".$this->pagination->create_links_ajax()."
          </ul>
        </div>
      </td>
    </tr>";
    die;
  }
  
  public function ajax_setting_biaya($total = 0, $start = 0){
    $this->global_models->get_connect("zikula");
    $data = $this->global_models->get_query("
      SELECT A.*
      FROM academicportal_kelompok_mahasiswa AS A
      ORDER BY A.id_kelompok_mahasiswa DESC LIMIT {$start},10
      ");
    $this->global_models->get_connect("default");
    foreach ($data as $key => $value) {
      
      print "
      <tr class='ajax-quotation'>
        <td>{$value->id_kelompok_mahasiswa}</td>
        <td>{$value->nama_kelompok}</td>
        <td>
          <div class='btn-group'>
            <button data-toggle='dropdown' class='btn btn-small dropdown-toggle'>Action<span class='caret'></span></button>
            <ul class='dropdown-menu'>
              <li><a href='".site_url("keuangan-mahasiswa/setting-biaya-angkatan/".$value->id_kelompok_mahasiswa)."'>Biaya Angkatan</a></li>
            </ul>
          </div>
        </td>
      </tr>";
        
    }
    
    $this->load->library('pagination');

    $config['base_url'] = '';
    $config['total_rows'] = $total;
    $config['per_page'] = 10; 
    $config['uri_segment'] = 4; 
    $config['cur_tag_open'] = "<li class='active'><a href='javascript:void(0)'>"; 
    $config['cur_tag_close'] = "</a></li>"; 
    $config['first_tag_open'] = "<li>"; 
    $config['first_tag_close'] = "</li>"; 
    $config['last_tag_open'] = "<li>"; 
    $config['last_tag_close'] = "</li>"; 
    $config['next_tag_open'] = "<li>"; 
    $config['next_tag_close'] = "</li>"; 
    $config['prev_tag_open'] = "<li>"; 
    $config['prev_tag_close'] = "</li>"; 
    $config['num_tag_open'] = "<li>"; 
    $config['num_tag_close'] = "</li>";
    $config['function_js'] = "load_quotation";

    $this->pagination->initialize($config); 
    print "
    <tr class='ajax-quotation'>
      <td colspan='6'>
        <div class='pagination'>
          <ul>
          ".$this->pagination->create_links_ajax()."
          </ul>
        </div>
      </td>
    </tr>";
    die;
  }
  
  function generate_tanggungan($nim){
    $this->global_models->get_connect("zikula");
    $current = $this->global_models->get("academicportal_semester_tahun_ajaran_berjalan", array("is_running" => 1));
    $this->global_models->get_connect("default");
    if($this->input->post(NULL)){
      $this->global_models->get_connect("zikula");
      $mahasiswa = $this->global_models->get_query("
        SELECT A.*, B.course_category
        FROM academicportal_mahasiswa AS A
        LEFT JOIN academicportal_student_categories AS B ON A.pn_uid = B.users
        WHERE A.nim = '{$nim}'
        ");
      
      $fakultas = $this->global_models->get_field("academicportal_course_categories", "parent", array("id" => $mahasiswa[0]->course_category));
      $this->global_models->get_connect("default");
//      ambil tunggakan saat ini
      $tunggakan = $this->mfrm->get_tunggakan($nim);
//      ambil biaya her registrasi
      $id_sem_ta = $this->get_current_sta($mahasiswa[0]->course_category);
      $her_registrasi = $this->global_models->get_field("keuangan_mahasiswa_setting_biaya_angkatan_cicilan", "percent", 
              array("id_kelompok_mahasiswa" => $mahasiswa[0]->kelompok_mahasiswa, "id_course_category" => $mahasiswa[0]->course_category, "type" => 0));
      $keuangan_mahasiswa_jenis_virtual_account = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_virtual_account_her_registrasi");
      $this->global_models->get_connect("zikula");
      $program_kuliah = $this->global_models->get("academicportal_mahasiswa", array("nim" => $mahasiswa[0]->nim));
      $this->global_models->get_connect("default");
      $kirim_tanggungan = array(
            "nim"                               => $mahasiswa[0]->nim,
            "id_keuangan_mahasiswa_jenis_biaya" => 0,
            "id_keuangan_mahasiswa_jenis_virtual_account" => $keuangan_mahasiswa_jenis_virtual_account[1],
            "id_program_kuliah"                 => $program_kuliah[0]->program_kuliah,
            "id_kelompok"                       => $program_kuliah[0]->kelompok_mahasiswa,
            "saldo"                             => $her_registrasi,
            "id_semester"                       => $id_sem_ta[0]->semester,
            "tahun"                             => $id_sem_ta[0]->tahun_ajaran,
            "tahunsemester"       => $id_sem_ta[0]->tahun_ajaran.$id_sem_ta[0]->semester,
            "pos"                               => 1,
            "notransaksi"                       => $notransaksi,
            "note"                              => "Tanggungan HER Registrasi ".$id_sem_ta[0]->semester." ".$id_sem_ta[0]->tahun_ajaran,
            "create_by_users"                   => $this->session->userdata("id"),
            "create_date"                       => date("Y-m-d H:i:s")
        );
      
//      $this->debug($mahasiswa);
//      $this->debug($kirim_tanggungan, true);
      
      $this->global_models->insert("keuangan_mahasiswa_tanggungan", $kirim_tanggungan);
      $account_her_regis = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_pendapatan_her_registrasi");
      $id_piutang_mahasiswa = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_fakultas_beban_mahasiswa_".$fakultas);
      $id_pembalik_kelebihan_bayar = $this->nbscache->get_explode("variable", "id_pembalik_kelebihan_bayar");
      
      $kirim_journal_log = array(
        "id_frm_journal"      => $this->mfrm->get_journal_current(),
        "notransaksi"         => $this->generate_tanggungan_notransaksi(),
        "saldo"               => $her_registrasi,
        "tanggal"             => date("Y-m-d"),
        "note"                => "Biaya Tanggungan ".$current[0]->semester." ".$current[0]->tahun_ajaran.
          " <a href='".site_url("keuangan-mahasiswa/kas-mahasiswa/".$nim)."'>{$nim}</a>",
      );
      $id_frm_journal_log = $this->mfrm->create_journal_log($kirim_journal_log);
      
      if($id_frm_journal_log){
        if($tunggakan >= 0){
          $debit[] = $her_registrasi;
          $debit_da[] = $id_piutang_mahasiswa[1];
        }
        else{
          if(($tunggakan + $her_registrasi) <= 0){
            $debit[] = $her_registrasi;
            $debit_da[] = $id_pembalik_kelebihan_bayar[1];
          }
          else{
            $debit[] = $tunggakan;
            $debit_da[] = $id_pembalik_kelebihan_bayar[1];
            $debit[] = abs(($tunggakan + $her_registrasi));
            $debit_da[] = $id_piutang_mahasiswa[1];
          }
        }

        $kredit[] = $her_registrasi;
        $kredit_da[] = $account_her_regis[1];
        $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);
      }
      redirect("keuangan-mahasiswa/kas-mahasiswa/".$nim);
    }
    else{
//      if($this->global_models->get_field("keuangan_mahasiswa_tanggungan", "sum(id_keuangan_mahasiswa_jenis_biaya)", 
//              array("nim" => $nim, "id_semester" => $current[0]->semester, "tahun" => $current[0]->tahun_ajaran)) > 0){
//        $this->session->set_flashdata('notice', 'Tanggungan Sudah Dibuat');
//        redirect("keuangan-mahasiswa/kas-mahasiswa/".$nim);
//      }
      $this->template->build("generate-tanggungan",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'keuangan-mahasiswa/mahasiswa',
              'title'       => lang("keuangan_mahasiswa_get_tanggungan"),
              'nim'         => $nim,
            ));
      $this->template
        ->set_layout('default')
        ->build("generate-tanggungan");
    }
  }
  
  function piutang_notransaksi(){
    return rand(111111, 999999);
  }
  function generate_tanggungan_notransaksi(){
    return rand(111111, 999999);
  }
  
  public function ajax_kas_mahasiswa($nim, $id_keuangan_mahasiswa_jenis_virtual_account = 0, $total = 0, $start = 0){
    
    if($id_keuangan_mahasiswa_jenis_virtual_account > 0){
      $q_tambahan = "AND A.id_keuangan_mahasiswa_jenis_virtual_account = {$id_keuangan_mahasiswa_jenis_virtual_account}";
    }
    
    $data = $this->global_models->get_query("
      SELECT A.*, B.title AS jenis_biaya
      FROM keuangan_mahasiswa_tanggungan AS A
      LEFT JOIN keuangan_mahasiswa_jenis_biaya AS B ON A.id_keuangan_mahasiswa_jenis_biaya = B.id_keuangan_mahasiswa_jenis_biaya
      WHERE A.nim = '{$nim}'
        {$q_tambahan}
      ORDER BY A.create_date DESC LIMIT {$start},10
      ");
      
    $total_sebelum = $this->global_models->get_query("
      SELECT A.*
      FROM keuangan_mahasiswa_tanggungan AS A
      LEFT JOIN keuangan_mahasiswa_jenis_biaya AS B ON A.id_keuangan_mahasiswa_jenis_biaya = B.id_keuangan_mahasiswa_jenis_biaya
      WHERE A.nim = '{$nim}'
        {$q_tambahan}
      ORDER BY A.create_date DESC LIMIT 0,{$start}
      ");
//    $this->debug($data);
//    $this->debug($total_sebelum, true);
    $total_debit = $this->global_models->get_query("
      SELECT SUM(A.saldo) AS saldo
      FROM keuangan_mahasiswa_tanggungan AS A
      WHERE A.nim = '{$nim}' AND (A.pos = 1 OR A.pos = 3)
        {$q_tambahan}
      ");
      
    $total_kredit = $this->global_models->get_query("
      SELECT SUM(A.saldo) AS saldo
      FROM keuangan_mahasiswa_tanggungan AS A
      WHERE A.nim = '{$nim}' AND A.pos = 2
        {$q_tambahan}
      ");
//    $this->debug($total_debit);
//    $this->debug($total_kredit, true);
    foreach ($total_sebelum AS $ts){
      if($ts->pos == 1){
        $total_debit_sebelum += $ts->saldo;
      }
      else{
        $total_kredit_sebelum += $ts->saldo;
      }
    }
    $saldo_sebelum = $total_debit_sebelum - $total_kredit_sebelum;
    $saldo_all = $total_debit[0]->saldo - $total_kredit[0]->saldo;
    $total_akhir = $saldo_all - $saldo_sebelum;
//    $this->debug($total_debit);
//    $this->debug($total_kredit, true);
//    $total_kredit = $this->global_models->get_field("frm_journal_log_account", "SUM(saldo)", 
//            array("id_frm_account" => $id_frm_account, "id_frm_journal" => $id_frm_journal, "pos" => 2, "tanggal >" => $data[0]->tanggal));
    
//    $pos_asli = $this->global_models->get_field("frm_account", "pos", array("id_frm_account" => $id_frm_account));
//    if($pos_asli == 1)
//      $total_akhir = $total_debit[0]->saldo - $total_kredit[0]->saldo;
//    else
//      $total_akhir = $total_kredit[0]->saldo - $total_debit[0]->saldo;
    
    foreach ($data as $key => $value) {
      if($value->pos == 1 OR $value->pos == 3){
        $saldo_debit = $value->saldo;
        $saldo_kredit = "";
        
      }
      else{
        $saldo_debit = "";
        $saldo_kredit = $value->saldo;
        
      }
      if($value->id_keuangan_mahasiswa_jenis_biaya){
        $title = "Tanggungan ".$value->jenis_biaya. " ". $value->id_semester." - ".$value->tahun;
      }
      else{
        $title = $value->note;
      }
      print "
      <tr class='ajax-quotation'>
        <td>{$value->create_date}</td>
        <td>{$value->tahunsemester}</td>
        <td>{$title}</td>
        <td>{$value->notransaksi}</td>
        <td style='text-align: right'>Rp ".  number_format($saldo_debit, 0, ",", ".")."</td>
        <td style='text-align: right'>Rp ".  number_format($saldo_kredit, 0, ",", ".")."</td>
        <td style='text-align: right; font-weight: bold;'>Rp ".  number_format($total_akhir, 0, ",", ".")."</td>
      </tr>";
      
      if($value->pos == 1 OR $value->pos == 3){
        $total_akhir = $total_akhir - $saldo_debit;
      }
      else{
        $total_akhir = $total_akhir + $saldo_kredit;
      }
    }
    
    $this->load->library('pagination');

    $config['base_url'] = '';
    $config['total_rows'] = $total;
    $config['per_page'] = 10; 
    $config['uri_segment'] = 6; 
    $config['cur_tag_open'] = "<li class='active'><a href='javascript:void(0)'>"; 
    $config['cur_tag_close'] = "</a></li>"; 
    $config['first_tag_open'] = "<li>"; 
    $config['first_tag_close'] = "</li>"; 
    $config['last_tag_open'] = "<li>"; 
    $config['last_tag_close'] = "</li>"; 
    $config['next_tag_open'] = "<li>"; 
    $config['next_tag_close'] = "</li>"; 
    $config['prev_tag_open'] = "<li>"; 
    $config['prev_tag_close'] = "</li>"; 
    $config['num_tag_open'] = "<li>"; 
    $config['num_tag_close'] = "</li>";
    $config['function_js'] = "load_quotation";

    $this->pagination->initialize($config); 
    print "
    <tr class='ajax-quotation'>
      <td colspan='6'>
        <div class='pagination'>
          <ul>
          ".$this->pagination->create_links_ajax()."
          </ul>
        </div>
      </td>
    </tr>";
    die;
  }
  
  public function ajax_kas_mahasiswa_baru($id_ao_applicant, $id_keuangan_mahasiswa_jenis_virtual_account = 0, $total = 0, $start = 0){
    
    if($id_keuangan_mahasiswa_jenis_virtual_account > 0){
      $q_tambahan = "AND A.id_keuangan_mahasiswa_jenis_virtual_account = {$id_keuangan_mahasiswa_jenis_virtual_account}";
    }
    
    $data = $this->global_models->get_query("
      SELECT A.*, B.title AS jenis_biaya
      FROM keuangan_mahasiswa_baru_tanggungan AS A
      LEFT JOIN keuangan_mahasiswa_jenis_biaya AS B ON A.id_keuangan_mahasiswa_jenis_biaya = B.id_keuangan_mahasiswa_jenis_biaya
      WHERE A.id_ao_applicant = '{$id_ao_applicant}'
        {$q_tambahan}
      ORDER BY A.create_date DESC LIMIT {$start},10
      ");
      
    $total_sebelum = $this->global_models->get_query("
      SELECT A.*
      FROM keuangan_mahasiswa_baru_tanggungan AS A
      LEFT JOIN keuangan_mahasiswa_jenis_biaya AS B ON A.id_keuangan_mahasiswa_jenis_biaya = B.id_keuangan_mahasiswa_jenis_biaya
      WHERE A.id_ao_applicant = '{$id_ao_applicant}'
        {$q_tambahan}
      ORDER BY A.create_date DESC LIMIT 0,{$start}
      ");
//    $this->debug($data);
//    $this->debug($total_sebelum);
    $total_debit = $this->global_models->get_query("
      SELECT SUM(A.saldo) AS saldo
      FROM keuangan_mahasiswa_baru_tanggungan AS A
      WHERE A.id_ao_applicant = '{$id_ao_applicant}' AND A.pos = 1
        {$q_tambahan}
      ");
      
    $total_kredit = $this->global_models->get_query("
      SELECT SUM(A.saldo) AS saldo
      FROM keuangan_mahasiswa_baru_tanggungan AS A
      WHERE A.id_ao_applicant = '{$id_ao_applicant}' AND A.pos = 2
        {$q_tambahan}
      ");
    
    foreach ($total_sebelum AS $ts){
      if($ts->pos == 1){
        $total_debit_sebelum += $ts->saldo;
      }
      else{
        $total_kredit_sebelum += $ts->saldo;
      }
    }
    $saldo_sebelum = $total_debit_sebelum - $total_kredit_sebelum;
    $saldo_all = $total_debit[0]->saldo - $total_kredit[0]->saldo;
    $total_akhir = $saldo_all - $saldo_sebelum;
//    $this->debug($total_debit);
//    $this->debug($total_kredit, true);
//    $total_kredit = $this->global_models->get_field("frm_journal_log_account", "SUM(saldo)", 
//            array("id_frm_account" => $id_frm_account, "id_frm_journal" => $id_frm_journal, "pos" => 2, "tanggal >" => $data[0]->tanggal));
    
//    $pos_asli = $this->global_models->get_field("frm_account", "pos", array("id_frm_account" => $id_frm_account));
//    if($pos_asli == 1)
//      $total_akhir = $total_debit[0]->saldo - $total_kredit[0]->saldo;
//    else
//      $total_akhir = $total_kredit[0]->saldo - $total_debit[0]->saldo;
    
    foreach ($data as $key => $value) {
      if($value->pos == 1){
        $saldo_debit = $value->saldo;
        $saldo_kredit = "";
        
      }
      else{
        $saldo_debit = "";
        $saldo_kredit = $value->saldo;
        
      }
      if($value->id_keuangan_mahasiswa_jenis_biaya){
        $title = "Tanggungan ".$value->jenis_biaya. " ". $value->id_semester." - ".$value->tahun;
      }
      else{
        $title = $value->note;
      }
      print "
      <tr class='ajax-quotation'>
        <td>{$value->create_date}</td>
        <td>{$title}</td>
        <td>{$value->notransaksi}</td>
        <td style='text-align: right'>Rp ".  number_format($saldo_debit, 0, ",", ".")."</td>
        <td style='text-align: right'>Rp ".  number_format($saldo_kredit, 0, ",", ".")."</td>
        <td style='text-align: right; font-weight: bold;'>Rp ".  number_format($total_akhir, 0, ",", ".")."</td>
      </tr>";
      
      if($value->pos == 1){
        $total_akhir = $total_akhir - $saldo_debit;
      }
      else{
        $total_akhir = $total_akhir + $saldo_kredit;
      }
    }
    
    $this->load->library('pagination');

    $config['base_url'] = '';
    $config['total_rows'] = $total;
    $config['per_page'] = 10; 
    $config['uri_segment'] = 6; 
    $config['cur_tag_open'] = "<li class='active'><a href='javascript:void(0)'>"; 
    $config['cur_tag_close'] = "</a></li>"; 
    $config['first_tag_open'] = "<li>"; 
    $config['first_tag_close'] = "</li>"; 
    $config['last_tag_open'] = "<li>"; 
    $config['last_tag_close'] = "</li>"; 
    $config['next_tag_open'] = "<li>"; 
    $config['next_tag_close'] = "</li>"; 
    $config['prev_tag_open'] = "<li>"; 
    $config['prev_tag_close'] = "</li>"; 
    $config['num_tag_open'] = "<li>"; 
    $config['num_tag_close'] = "</li>";
    $config['function_js'] = "load_quotation";

    $this->pagination->initialize($config); 
    print "
    <tr class='ajax-quotation'>
      <td colspan='6'>
        <div class='pagination'>
          <ul>
          ".$this->pagination->create_links_ajax()."
          </ul>
        </div>
      </td>
    </tr>";
    die;
  }
  
  public function setting_biaya(){
    $this->global_models->get_connect("zikula");
    $total = $this->global_models->get_query("
      SELECT count(A.id_kelompok_mahasiswa) AS jumlah
      FROM academicportal_kelompok_mahasiswa AS A
      ");
    $this->global_models->get_connect("default");  
    $this->template->build("setting-biaya",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-mahasiswa/setting-biaya',
            'title'       => lang("keuangan_mahasiswa_setting_biaya"),
            'total'       => $total[0]->jumlah
          ));
    $this->template
      ->set_layout('default')
      ->build("setting-biaya");
	}
  
  function auto_jenis_biaya(){
    if (empty($_GET['term'])) exit ;
    $q = strtolower($_GET["term"]);
    if (get_magic_quotes_gpc()) $q = stripslashes($q);
    $items = $this->global_models->get_query("
      SELECT *
      FROM keuangan_mahasiswa_jenis_biaya
      WHERE
      (LOWER(title) LIKE '%{$q}%' OR LOWER(code) LIKE '%{$q}%')
      LIMIT 0,10
      ");
    if(count($items) > 0){
      foreach($items as $tms){
        $result[] = array(
            "id"    => $tms->id_keuangan_mahasiswa_jenis_biaya,
            "label" => $tms->title." - ".$tms->code,
            "value" => $tms->title." - ".$tms->code,
        );
      }
    }
    else{
      $result[] = array(
          "id"    => 0,
          "label" => "No Found",
          "value" => "No Found",
      );
    }
    echo json_encode($result);
    die;
  }
  
  function auto_nim_prokul($id_program_kuliah){
    if (empty($_GET['term'])) exit ;
    $q = strtolower($_GET["term"]);
    if (get_magic_quotes_gpc()) $q = stripslashes($q);
    $this->global_models->get_connect("zikula");
    $items = $this->global_models->get_query("
      SELECT nim, nama
      FROM academicportal_mahasiswa
      WHERE
      (LOWER(nim) LIKE '%{$q}%') AND program_kuliah = {$id_program_kuliah}
      LIMIT 0,10
      ");
    $this->global_models->get_connect("default");
    if(count($items) > 0){
      foreach($items as $tms){
        $result[] = array(
            "id"    => $tms->nim,
            "label" => $tms->nim." - ".$tms->nama,
            "value" => $tms->nim." - ".$tms->nama,
        );
      }
    }
    else{
      $result[] = array(
          "id"    => 0,
          "label" => "No Found",
          "value" => "No Found",
      );
    }
    echo json_encode($result);
    die;
  }
  
  function auto_jenis_biaya_masuk(){
    if (empty($_GET['term'])) exit ;
    $q = strtolower($_GET["term"]);
    if (get_magic_quotes_gpc()) $q = stripslashes($q);
    $items = $this->global_models->get_query("
      SELECT *
      FROM keuangan_mahasiswa_jenis_biaya_masuk
      WHERE
      (LOWER(title) LIKE '%{$q}%' OR LOWER(code) LIKE '%{$q}%')
      LIMIT 0,10
      ");
    if(count($items) > 0){
      foreach($items as $tms){
        $result[] = array(
            "id"    => $tms->id_keuangan_mahasiswa_jenis_biaya_masuk,
            "label" => $tms->title." - ".$tms->code,
            "value" => $tms->title." - ".$tms->code,
        );
      }
    }
    else{
      $result[] = array(
          "id"    => 0,
          "label" => "No Found",
          "value" => "No Found",
      );
    }
    echo json_encode($result);
    die;
  }
  
  function tanggungan_mahasiswa($id_course_category){
    
    $ta_current = $this->get_current_sta();
    
    $query = "";
    if($id_course_category > 0){
      $query = "AND course_category = $id_course_category";
      $ta_current = $this->get_current_sta($id_course_category);
    }
//    $this->debug($ta_current, true);
    $this->global_models->get_connect("zikula");
    $jumlah_mahasiswa = $this->global_models->get_query("
      SELECT count(users) AS jumlah
      FROM academicportal_student_categories
      WHERE (student_status = 'A' OR student_status = 'C' OR student_status = 'N')
      AND (flag_keuangan <> {$ta_current[0]->semester}{$ta_current[0]->tahun_ajaran}1 OR flag_keuangan IS NULL)
        $query
      ");
    
    $this->global_models->get_connect("default");  
//    $this->debug($jumlah_mahasiswa, true);
    $set_ses = array(
          "keuangan_mahasiswa_generate_tanggungan_mahasiswa_1"  => 0,
          "keuangan_mahasiswa_generate_tanggungan_mahasiswa_2"  => $jumlah_mahasiswa[0]->jumlah,
          "keuangan_mahasiswa_total_taggungan"                  => 0,
          "keuangan_mahasiswa_tunggakan_bayar_muka"             => 0,
      );
    $this->session->set_userdata($set_ses);
    $this->template->build("tanggungan-mahasiswa",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-mahasiswa/tanggungan-mahasiswa-pilihan',
            'title'       => "Pilih Batas Generate",
            'id_course_category' => $id_course_category,
            'jumlah'      => $jumlah_mahasiswa[0]->jumlah
          ));
    $this->template
      ->set_layout('default')
      ->build("tanggungan-mahasiswa");
  }
  
  function tanggungan_mahasiswa_pilihan(){
    $this->global_models->get_connect("zikula");
    
    $course_category = $this->global_models->get_query("
      SELECT *
      FROM academicportal_course_categories
      WHERE set_as_program_jurusan = 1
      ");
    
    $this->global_models->get_connect("default");  
//    $this->debug($jumlah_mahasiswa, true);
    $this->template->build("tanggungan-mahasiswa-pilihan",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-mahasiswa/tanggungan-mahasiswa-pilihan',
            'title'       => "Pilih Batas Generate",
            'course_category' => $course_category
          ));
    $this->template
      ->set_layout('default')
      ->build("tanggungan-mahasiswa-pilihan");
  }
  
  function generate_tanggungan_mahasiswa($type){
    
//    $set_ses = array(
//          "keuangan_mahasiswa_generate_tanggungan_mahasiswa_1"  => 0,
//          "keuangan_mahasiswa_generate_tanggungan_mahasiswa_2"  => $jumlah_mahasiswa[0]->jumlah,
//          "keuangan_mahasiswa_total_taggungan"                  => 0,
//          "keuangan_mahasiswa_tunggakan_bayar_muka"             => 0,
//      );
//    $this->session->set_userdata($set_ses);
    
//    $this->debug($jumlah_mahasiswa, true);
    
    $this->template->build("generate-tanggungan-mahasiswa-batch",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-mahasiswa/tanggungan-mahasiswa-pilihan',
            'title'       => lang("keuangan_mahasiswa_generate_tanggungan"),
            'type'        => $type
          ));
    $this->template
      ->set_layout('default')
      ->build("generate-tanggungan-mahasiswa-batch");
  }
  
  function batch_generate_tanggungan_mahasiswa($course_category_kirim){
    
    $ta_current = $this->get_current_sta();
    $query = "";
    if($course_category_kirim > 0){
      $query = "AND A.course_category = $course_category_kirim";
    }
    $this->global_models->get_connect("zikula");
    
    $mahasiswa = $this->global_models->get_query("
      SELECT A.*, B.tahun_masuk, B.nim
      FROM academicportal_student_categories AS A
      LEFT JOIN academicportal_mahasiswa AS B ON A.users = B.pn_uid
      WHERE (A.student_status = 'A' OR A.student_status = 'C' OR A.student_status = 'N')
      AND (A.flag_keuangan <> {$ta_current[0]->semester}{$ta_current[0]->tahun_ajaran}1 OR A.flag_keuangan IS NULL)
        $query
      ORDER BY A.users ASC LIMIT 0, 1
      ");
    $program_kuliah = $this->global_models->get("academicportal_mahasiswa", array("nim" => $mahasiswa[0]->nim));
    $this->global_models->get_connect("default");
//    $this->debug("SELECT A.*, B.tahun_masuk, B.nim
//      FROM academicportal_student_categories AS A
//      LEFT JOIN academicportal_mahasiswa AS B ON A.users = B.pn_uid
//      WHERE (A.student_status = 'A' OR A.student_status = 'C' OR A.student_status = 'N')
//      AND (A.flag_keuangan <> {$ta_current[0]->semester}{$ta_current[0]->tahun_ajaran}1 OR A.flag_keuangan IS NULL)
//      ORDER BY A.users ASC LIMIT {$this->session->userdata("keuangan_mahasiswa_generate_tanggungan_mahasiswa_1")}, 1", true);
    $total_debit = $this->global_models->get_query("
      SELECT SUM(A.saldo) AS saldo
      FROM keuangan_mahasiswa_tanggungan AS A
      WHERE A.nim = '{$mahasiswa[0]->nim}' AND A.pos = 1
      ");

    $total_kredit = $this->global_models->get_query("
      SELECT SUM(A.saldo) AS saldo
      FROM keuangan_mahasiswa_tanggungan AS A
      WHERE A.nim = '{$mahasiswa[0]->nim}' AND A.pos = 2
      ");

    $total_akhir = $total_kredit[0]->saldo - $total_debit[0]->saldo;
    
    
    if ($mahasiswa) {
      if($this->session->userdata("keuangan_mahasiswa_generate_tanggungan_mahasiswa_1") < 1){
        $nomor = 1;
      }
      else{
        $nomor = $this->session->userdata("keuangan_mahasiswa_generate_tanggungan_mahasiswa_1") + 1;
      }
      $id_sem_ta = $this->get_current_sta($mahasiswa[0]->course_category);
      $semester_ke = ((($id_sem_ta[0]->tahun_ajaran - $mahasiswa[0]->tahun_masuk) * 2) + $id_sem_ta[0]->semester);
//      set keuangan
//      get her registrasi
      $notransaksi = $this->piutang_notransaksi();
      if($semester_ke > 1){
        $her_registrasi = $this->global_models->get_field("keuangan_mahasiswa_setting_biaya_angkatan_cicilan", "percent", 
              array("id_kelompok_mahasiswa" => $mahasiswa[0]->kelompok_mahasiswa, "id_course_category" => $mahasiswa[0]->course_category, "type" => 0));
        $keuangan_mahasiswa_virtual_account_her_registrasi = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_virtual_account_her_registrasi");
        $keuangan_mahasiswa_virtual_account_biaya_kuliah = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_virtual_account_biaya_kuliah");
  //      $her_registrasi = 50000;
        $kirim_tanggungan[] = array(
            "nim"                               => $mahasiswa[0]->nim,
            "id_keuangan_mahasiswa_jenis_biaya" => 0,
            "id_keuangan_mahasiswa_jenis_virtual_account" => $keuangan_mahasiswa_virtual_account_her_registrasi[1],
            "saldo"                             => $her_registrasi,
            "id_semester"                       => $id_sem_ta[0]->semester,
            "tahun"                             => $id_sem_ta[0]->tahun_ajaran,
            "pos"                               => 1,
            "notransaksi"                       => $notransaksi,
            "note"                              => "Tanggungan HER Registrasi ".$id_sem_ta[0]->semester." ".$id_sem_ta[0]->tahun_ajaran,
            
            "id_program_kuliah"                 => $program_kuliah[0]->program_kuliah,
            "id_kelompok"                       => $program_kuliah[0]->kelompok_mahasiswa,
            "id_semester"                       => $id_sem_ta[0]->semester,
            "tahun"                             => $id_sem_ta[0]->tahun_ajaran,
            "tahunsemester"       => $id_sem_ta[0]->tahun_ajaran.$id_sem_ta[0]->semester,
            
            "create_by_users"                   => $this->session->userdata("id"),
            "create_date"                       => date("Y-m-d H:i:s")
        );
      
      
//      get semester ke
        if($id_sem_ta[0]->semester <= 2){
  //        $semester_ke = ((($id_sem_ta[0]->tahun_ajaran - $mahasiswa[0]->tahun_masuk) * 2) + $id_sem_ta[0]->semester) - 1;

          $percent_cicilan_kuliah = $this->global_models->get_field("keuangan_mahasiswa_setting_biaya_angkatan_cicilan_kuliah", "percent", 
                  array("id_kelompok_mahasiswa" => $mahasiswa[0]->kelompok_mahasiswa, "id_course_category" => $mahasiswa[0]->course_category, "semester" => $semester_ke));

          //      get cicilan tahunan

          if($percent_cicilan_kuliah > 0){

  //          ambil jenis biaya
            $jenis_biaya_kuliah = $this->global_models->get("keuangan_mahasiswa_jenis_biaya_kuliah");

            $data_cache_biaya_kuliah = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_total_biaya_kuliah");
            $cache_total_biaya_kuliah = unserialize($data_cache_biaya_kuliah[1]);

            foreach($jenis_biaya_kuliah AS $jbk){
              $total_biaya_kuiah_temp = $this->global_models->get_query("
              SELECT SUM(price) AS total
              FROM keuangan_mahasiswa_setting_biaya_angkatan
              WHERE id_kelompok_mahasiswa = {$mahasiswa[0]->kelompok_mahasiswa} AND
                id_course_category = {$mahasiswa[0]->course_category} AND
                id_keuangan_mahasiswa_jenis_biaya_kuliah = {$jbk->id_keuangan_mahasiswa_jenis_biaya_kuliah}
              ");

              $total_biaya_kuiah[$jbk->id_frm_account] = $total_biaya_kuiah_temp[0]->total * $percent_cicilan_kuliah/100;
              $tunggakan_kuliah += $total_biaya_kuiah[$jbk->id_frm_account];

  //            simpan ke cache untuk eksekusi journal
              $cache_total_biaya_kuliah[$jbk->id_frm_account] += $total_biaya_kuiah[$jbk->id_frm_account];
            }
            $this->nbscache->put_tunggal("variable", "keuangan_mahasiswa_total_biaya_kuliah", serialize($cache_total_biaya_kuliah));
  //          $total_biaya_kuiah = $this->global_models->get_query("
  //            SELECT SUM(price) AS total
  //            FROM keuangan_mahasiswa_setting_biaya_angkatan
  //            WHERE id_kelompok_mahasiswa = {$mahasiswa[0]->kelompok_mahasiswa} AND
  //              id_course_category = {$mahasiswa[0]->course_category} AND
  //              id_keuangan_mahasiswa_jenis_biaya_kuliah > 0
  //            ");
  //          $tunggakan_kuliah = $total_biaya_kuiah[0]->total * $percent_cicilan_kuliah/100;
            
            $kirim_tanggungan[] = array(
                "nim"                               => $mahasiswa[0]->nim,
                "id_keuangan_mahasiswa_jenis_biaya" => 0,
                "id_program_kuliah"                 => $program_kuliah[0]->program_kuliah,
                "id_kelompok"                       => $program_kuliah[0]->kelompok_mahasiswa,
                "id_keuangan_mahasiswa_jenis_virtual_account" => $keuangan_mahasiswa_virtual_account_biaya_kuliah[1],
                "saldo"                             => $tunggakan_kuliah,
                "id_semester"                       => $id_sem_ta[0]->semester,
                "tahun"                             => $id_sem_ta[0]->tahun_ajaran,
                "tahunsemester"       => $id_sem_ta[0]->tahun_ajaran.$id_sem_ta[0]->semester,
                "pos"                               => 1,
                "notransaksi"                       => $notransaksi,
                "note"                              => "Tanggungan Cicilan Biaya Kuliah ke ".$semester_ke,
                "create_by_users"                   => $this->session->userdata("id"),
                "create_date"                       => date("Y-m-d H:i:s")
            );

          }
        }
      
        $this->global_models->insert_batch("keuangan_mahasiswa_tanggungan", $kirim_tanggungan);

        $saldo_debit = $her_registrasi;
      
//      $this->debug($mahasiswa);
//      $this->debug($saldo_debit);
//      $this->debug($total_akhir, true);
        if(($total_akhir - $saldo_debit) >= 0){
  //        if(($total_akhir - $saldo_debit) > 0){
  //          die;
  //        }
          $this->global_models->get_connect("zikula");
          $id_persyaratan_keuangan = $this->global_models->get_field("academicportal_persyaratan_keuangan", "id", 
                  array("uid" => $mahasiswa[0]->users));

          if(!$id_persyaratan_keuangan){
            $this->global_models->insert("academicportal_persyaratan_keuangan", 
            array("uid" => $mahasiswa[0]->users, "id_sem_ta" => $id_sem_ta[0]->id, "insert_by_who" => $this->session->userdata("id"), "update_by_who" => -1, 
                "delete_by_who" => -1));
          }
          else{
            $this->global_models->update("academicportal_persyaratan_keuangan", array("id" => $id_persyaratan_keuangan),
            array("uid" => $mahasiswa[0]->users, "id_sem_ta" => $id_sem_ta[0]->id, "insert_by_who" => $this->session->userdata("id"), "update_by_who" => -1, 
                "delete_by_who" => -1));
          }

          $this->global_models->get_connect("default");

          $set_ses = array(
              "keuangan_mahasiswa_generate_tanggungan_mahasiswa_1"  => $nomor,
              "keuangan_mahasiswa_tunggakan_bayar_muka" => ($saldo_debit + $this->session->userdata("keuangan_mahasiswa_tunggakan_bayar_muka")),
              "keuangan_mahasiswa_total_taggungan"      => $this->session->userdata("keuangan_mahasiswa_total_taggungan")
          );

          $keuangan_mahasiswa_total_taggungan_awal = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_total_taggungan");
          $keuangan_mahasiswa_total_taggungan = $keuangan_mahasiswa_total_taggungan_awal[1];

          $keuangan_mahasiswa_tunggakan_bayar_muka_awal = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_tunggakan_bayar_muka");
          $keuangan_mahasiswa_tunggakan_bayar_muka = $keuangan_mahasiswa_tunggakan_bayar_muka_awal[1] + $saldo_debit;
  //        $this->debug($set_ses, true);
        }
        elseif($total_akhir > 0 AND $total_akhir < $saldo_debit){
  //        $this->debug("2", true);
          $this->global_models->get_connect("zikula");
          $id_persyaratan_keuangan = $this->global_models->get_field("academicportal_persyaratan_keuangan", "id", 
                  array("uid" => $mahasiswa[0]->users));

          if(!$id_persyaratan_keuangan){
            $this->global_models->insert("academicportal_persyaratan_keuangan", 
            array("uid" => $mahasiswa[0]->users, "id_sem_ta" => $id_sem_ta[0]->id, "insert_by_who" => $this->session->userdata("id"), "update_by_who" => -1, 
                "delete_by_who" => $mahasiswa[0]->users));
          }
          else{
            $this->global_models->update("academicportal_persyaratan_keuangan", array("id" => $id_persyaratan_keuangan),
            array("uid" => $mahasiswa[0]->users, "id_sem_ta" => $id_sem_ta[0]->id, "insert_by_who" => $this->session->userdata("id"), "update_by_who" => -1, 
                "delete_by_who" => $mahasiswa[0]->users));
          }

          $this->global_models->get_connect("default");

          $set_ses = array(
              "keuangan_mahasiswa_generate_tanggungan_mahasiswa_1"  => $nomor,
              "keuangan_mahasiswa_tunggakan_bayar_muka" => ($total_akhir + $this->session->userdata("keuangan_mahasiswa_tunggakan_bayar_muka")),
              "keuangan_mahasiswa_total_taggungan"      => (($saldo_debit - $total_akhir) + $this->session->userdata("keuangan_mahasiswa_total_taggungan"))
          );

          $keuangan_mahasiswa_total_taggungan_awal = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_total_taggungan");
          $keuangan_mahasiswa_total_taggungan = ($saldo_debit - $total_akhir) + $keuangan_mahasiswa_total_taggungan_awal[1];

          $keuangan_mahasiswa_tunggakan_bayar_muka_awal = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_tunggakan_bayar_muka");
          $keuangan_mahasiswa_tunggakan_bayar_muka = $total_akhir + $keuangan_mahasiswa_tunggakan_bayar_muka_awal[1];
        }
        elseif(($total_akhir - $saldo_debit) < 0 AND $total_akhir <= 0){
  //        $this->debug("3", true);
          $this->global_models->get_connect("zikula");
          $id_persyaratan_keuangan = $this->global_models->get_field("academicportal_persyaratan_keuangan", "id", 
                  array("uid" => $mahasiswa[0]->users));

          if(!$id_persyaratan_keuangan){
            $this->global_models->insert("academicportal_persyaratan_keuangan", 
            array("uid" => $mahasiswa[0]->users, "id_sem_ta" => $id_sem_ta[0]->id, "insert_by_who" => $this->session->userdata("id"), "update_by_who" => -1, 
                "delete_by_who" => $mahasiswa[0]->users));
          }
          else{
            $this->global_models->update("academicportal_persyaratan_keuangan", array("id" => $id_persyaratan_keuangan),
            array("uid" => $mahasiswa[0]->users, "id_sem_ta" => $id_sem_ta[0]->id, "insert_by_who" => $this->session->userdata("id"), "update_by_who" => -1, 
                "delete_by_who" => $mahasiswa[0]->users));
          }

          $this->global_models->get_connect("default");

          $set_ses = array(
              "keuangan_mahasiswa_generate_tanggungan_mahasiswa_1"  => $nomor,
              "keuangan_mahasiswa_tunggakan_bayar_muka" => $this->session->userdata("keuangan_mahasiswa_tunggakan_bayar_muka"),
              "keuangan_mahasiswa_total_taggungan"      => ($saldo_debit + $this->session->userdata("keuangan_mahasiswa_total_taggungan"))
          );

          $keuangan_mahasiswa_total_taggungan_awal = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_total_taggungan");
          $keuangan_mahasiswa_total_taggungan = $saldo_debit + $keuangan_mahasiswa_total_taggungan_awal[1];

          $keuangan_mahasiswa_tunggakan_bayar_muka_awal = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_tunggakan_bayar_muka");
          $keuangan_mahasiswa_tunggakan_bayar_muka = $keuangan_mahasiswa_tunggakan_bayar_muka_awal[1];
        }
//      $this->debug($set_ses);
        $this->session->set_userdata($set_ses);

        $this->nbscache->put_tunggal("variable", "keuangan_mahasiswa_total_taggungan", $keuangan_mahasiswa_total_taggungan);
        $this->nbscache->put_tunggal("variable", "keuangan_mahasiswa_tunggakan_bayar_muka", $keuangan_mahasiswa_tunggakan_bayar_muka);

        $hasil = array(
            "percent" => ($nomor/$this->session->userdata("keuangan_mahasiswa_generate_tanggungan_mahasiswa_2") * 100),
            "n"       => $nomor,
            "rn"      => $this->session->userdata("keuangan_mahasiswa_generate_tanggungan_mahasiswa_2"),
            "note"    => trim($mahasiswa[0]->nim)."--".$set_ses["keuangan_mahasiswa_total_taggungan"]." -- ".$set_ses["keuangan_mahasiswa_tunggakan_bayar_muka"]."[$semester_ke]"
        );
      }
      else{
        $set_ses = array(
              "keuangan_mahasiswa_generate_tanggungan_mahasiswa_1"  => $nomor,
          );
        $this->session->set_userdata($set_ses);
        $hasil = array(
            "percent" => ($nomor/$this->session->userdata("keuangan_mahasiswa_generate_tanggungan_mahasiswa_2") * 100),
            "n"       => $nomor,
            "rn"      => $this->session->userdata("keuangan_mahasiswa_generate_tanggungan_mahasiswa_2"),
            "note"    => trim($mahasiswa[0]->nim)."--".$set_ses["keuangan_mahasiswa_total_taggungan"]." -- ".$set_ses["keuangan_mahasiswa_tunggakan_bayar_muka"]."[$semester_ke]"
        );
      }
      
      $this->global_models->get_connect("zikula");
      $this->global_models->update("academicportal_student_categories", array("id" => $mahasiswa[0]->id), array("flag_keuangan" => $ta_current[0]->semester.$ta_current[0]->tahun_ajaran."1"));
      $this->global_models->get_connect("default");
      
      print json_encode($hasil);
      die;
    }
  }
  
  function tanggungan_to_journal_mahasiswa($course_category){
    
    $prodi = "All";
    if($course_category > 0){
      $this->global_models->get_connect("zikula");
      $prodi = $this->global_models->get_field("academicportal_course_categories", "name", array("id" => $course_category));
      $fakultas = $this->global_models->get_field("academicportal_course_categories", "parent", array("id" => $course_category));
      $this->global_models->get_connect("default");
    }
    
    $keuangan_mahasiswa_total_taggungan_awal = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_total_taggungan");
    
    $keuangan_mahasiswa_tunggakan_bayar_muka_awal = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_tunggakan_bayar_muka");
    
    $saldo_debit = $keuangan_mahasiswa_total_taggungan_awal[1];;
    $total_akhir = $keuangan_mahasiswa_tunggakan_bayar_muka_awal[1];
    $tanggungan = $saldo_debit + $total_akhir;
    
//    tambahkan biaya kuliah di journal
    $data_cache_biaya_kuliah = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_total_biaya_kuliah");
    $cache_total_biaya_kuliah = unserialize($data_cache_biaya_kuliah[1]);
    
    foreach($cache_total_biaya_kuliah as $id_account_biaya_kuliah => $ctbk){
      $kredit[] = $ctbk;
      $kredit_da[] = $id_account_biaya_kuliah;
      $tanggungan_all += $ctbk;
    }
    
    $current = $this->get_current_sta();
    
    $kirim_journal_log = array(
        "id_frm_journal"      => $this->mfrm->get_journal_current(),
        "notransaksi"         => $this->generate_tanggungan_notransaksi(),
        "saldo"               => ($tanggungan_all + $tanggungan),
        "tanggal"             => date("Y-m-d"),
        "note"                => "Biaya Tanggungan ".$current[0]->semester." ".$current[0]->tahun_ajaran." ".$prodi,
    );
//      $id_frm_journal_log = 34;


    $id_frm_journal_log = $this->mfrm->create_journal_log($kirim_journal_log);

    if($id_frm_journal_log){
//        print $this->mfrm->get_tunggakan($pst['nim']);
      $debit[] = $saldo_debit;
//      $id_piutang_mahasiswa = $this->nbscache->get_explode("variable", "id_piutang_mahasiswa");
      $id_piutang_mahasiswa = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_fakultas_beban_mahasiswa_".$fakultas);
      $debit_da[] = $id_piutang_mahasiswa[1];
      
      $debit[] = $total_akhir;
//      $id_hutang_mahasiswa = $this->nbscache->get_explode("variable", "id_hutang_mahasiswa");
      $id_pembalik_kelebihan_bayar = $this->nbscache->get_explode("variable", "id_pembalik_kelebihan_bayar");
      $debit_da[] = $id_pembalik_kelebihan_bayar[1];

      $kredit[] = $tanggungan;
      $id_pendapatan_mahasiswa = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_pendapatan_her_registrasi");
      $kredit_da[] = $id_pendapatan_mahasiswa[1];
//        $this->debug($debit);
//        $this->debug($debit_da, true);
      $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);

      $this->session->set_flashdata('success', 'Data tersimpan');
    }
    else{
      $this->global_models->get_connect("zikula");
      $this->session->set_flashdata('notice', 'Perlu mengatur biaya angkatan '.$this->global_models->get_field("academicportal_kelompok_mahasiswa", "nama_kelompok", array("id_kelompok_mahasiswa" => $id_kelompok_mahasiswa)));
      $this->global_models->get_connect("default");
    }
    
    $set_ses = array(
          "keuangan_mahasiswa_generate_tanggungan_mahasiswa_1"  => 0,
          "keuangan_mahasiswa_generate_tanggungan_mahasiswa_2"  => 0,
          "keuangan_mahasiswa_total_taggungan"                  => 0,
          "keuangan_mahasiswa_tunggakan_bayar_muka"             => 0,
      );
    $this->session->set_userdata($set_ses);
    $this->nbscache->put_tunggal("variable", "keuangan_mahasiswa_total_taggungan", 0);
    $this->nbscache->put_tunggal("variable", "keuangan_mahasiswa_tunggakan_bayar_muka", 0);
    $this->nbscache->put_tunggal("variable", "keuangan_mahasiswa_total_biaya_kuliah", "");
    print "done";
    die;
  }
  
  private function get_current_sta_prodi($prokul = 0, $category_id = ""){
    $now = date("Y/m/d");
	
    $sql = "SELECT 	B.id AS id_sta,
					A.id,
					A.semester_name,
					A.semester_description,
					B.tahun_ajaran,
					B.semester_start,
					B.semester_finish,
					B.classreg_start,
					B.classreg_finish,
					B.classreg_add_drop_start,
					B.classreg_add_drop_finish,
					B.payment_start,
					B.payment_finish,
					B.payment_late_fee,
					B.grade_start,
					B.grade_finish,
					B.semester
			FROM	academicportal_semester AS A, academicportal_semester_tahun_ajaran AS B
			WHERE	B.program_kuliah = '$prokul'";
			
			if($category_id!=''){
				$sql .=" AND B.category = '$category_id'";
			}
			
			$sql .=" AND B.semester = A.id AND
					'$now' >= B.semester_start AND
					'$now' <= B.semester_finish";
      
      $this->global_models->get_connect("zikula");
      $data = $this->global_models->get_query($sql);
      $this->global_models->get_connect("default");
      return $data;
  }
  
  private function get_current_sta($category = 0){
    
    $sekarang = date("Y-m-d");
//    $sekarang = "2013-08-15";
    
    $this->global_models->get_connect("zikula");
    
    if($category > 0){
      $cat = " AND A.category = '$category' ";
    }
    else{
      $cat = " ";
    }
    
    $sql = "
      SELECT
        A.*
      FROM 
        academicportal_semester_tahun_ajaran AS A
          LEFT JOIN academicportal_semester AS B
            ON A.semester = B.id
      WHERE
        '".$sekarang."' >= A.semester_start AND
        '".$sekarang."' <= A.semester_finish 
          {$cat}
      ORDER BY A.tahun_ajaran DESC, A.semester DESC LIMIT 1
    ";
//    $sql = "
//      SELECT
//        A.*
//      FROM 
//        academicportal_semester_tahun_ajaran AS A
//          LEFT JOIN academicportal_semester AS B
//            ON A.semester = B.id
//      WHERE
//        '".$sekarang."' >= A.semester_start AND
//        '".$sekarang."' <= A.semester_finish 
//          {$cat}
//      ORDER BY A.tahun_ajaran DESC, A.semester DESC LIMIT 1
//    ";
    $data = $this->global_models->get_query($sql);
    $this->global_models->get_connect("default");
    return $data;
  }
  
  function addrow_biaya_kuliah(){
    $no = $this->input->post("no");
    $isi = $this->form_eksternal->form_input("semester[]", "");
    print <<<EOD
    <tr id='tr_{$no}'>
      <td>{$isi}</td>
    <td><a href='javascript:void(0)' class='remove-element' onclick='del_row_tambahan("tr_{$no}")' title='Remove' >Remove</a></td></tr>
EOD;
    die;
  }
  
  function tanggungan_after_krs(){
//    uid
//    sks
    
    $pst = $this->input->post();
    
    $this->global_models->get_connect("zikula");
    $mahasiswa = $this->global_models->get_query("
      SELECT A.*, B.nim
      FROM academicportal_student_categories AS A.
      LEFT JOIN academicportal_mahasiswa AS B ON A.users = B.pn_uid
      WHERE A.users = {$pst['users']}
      ");
    
    print "Berhasil";
    print "Gagal";
  }
  
  
  
  
  function pass_status_keuangan_mahasiswa($nim){
    $this->global_models->get_connect("zikula");
    $mahasiswa = $this->global_models->get_query("
        SELECT A.users
        FROM academicportal_student_categories AS A
        LEFT JOIN academicportal_mahasiswa AS B ON A.users = B.pn_uid
        WHERE B.nim = '{$nim}'");
    $this->global_models->update("academicportal_persyaratan_keuangan", array("uid" => $mahasiswa[0]->users),
            array("delete_by_who" => -1));
    $this->global_models->get_connect("default");
    redirect("keuangan-mahasiswa/mahasiswa");
  }
  
  function pass_status_keuangan_mahasiswa_baru($nim){
    $this->global_models->get_connect("zikula");
          $this->global_models->update("academicportal_ao_applicant", array("ref_number" => $nim),
                  array("status_uang_pangkal" => 2));
    $this->global_models->get_connect("default");
    redirect("keuangan-mahasiswa/mahasiswa-baru");
  }
  
  function export_btn_format(){
    if($this->input->post()){
      $this->execution_export_xls("Data BTN", $this->input->post("course_category"), $this->input->post("kelompok"), $this->input->post("id_keuangan_mahasiswa_jenis_virtual_account"));
    }
    else{
      $this->global_models->get_connect("zikula");
      $list_course_category = $this->global_models->get_dropdown("academicportal_course_categories", "id", "name", FALSE, 
              array("set_as_program_jurusan >" => 0));
      $list_kelompok = $this->global_models->get_dropdown("academicportal_kelompok_mahasiswa", "id_kelompok_mahasiswa", "nama_kelompok", FALSE);
      $this->global_models->get_connect("default");
      $this->template->build("export-btn-format",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'keuangan-mahasiswa/mahasiswa',
              'title'       => "Export BTN Format",
              'list_course_category'  => $list_course_category,
              'list_kelompok' => $list_kelompok
            ));
      $this->template
        ->set_layout('default')
        ->build("export-btn-format");
    }
  }
  
  function execution_export_xls($filename, $course_category, $kelompok, $id_keuangan_mahasiswa_jenis_virtual_account){
    
    $sta = $this->get_current_sta($course_category);
//    $this->debug($sta, true);
    $kode_ganjil= array(
        1 => 'GJL001',
        2 => 'GNP002',
    );
    $this->global_models->get_connect("zikula");
    $mahasiswa = $this->global_models->get_query("
        SELECT A.*, B.nim, B.nama, C.name AS prodi, D.name AS fakultas, E.nama_kelompok
        FROM academicportal_student_categories AS A
        LEFT JOIN academicportal_mahasiswa AS B ON A.users = B.pn_uid
        LEFT JOIN academicportal_course_categories AS C ON A.course_category = C.id
        LEFT JOIN academicportal_course_categories AS D ON C.kode_fak = D.kode_fak
        LEFT JOIN academicportal_kelompok_mahasiswa AS E ON A.kelompok_mahasiswa = E.id_kelompok_mahasiswa
        WHERE A.course_category = $course_category AND A.student_status NOT IN ('D','K', 'L') 
          AND A.kelompok_mahasiswa = $kelompok
        GROUP BY B.nim
        ORDER BY B.nim ASC
            ");
    $this->global_models->get_connect("default");
//    $this->debug($mahasiswa, true);
      $objPHPExcel = $this->phpexcel;
      $objPHPExcel->getProperties()->setCreator("Nugroho Budhi Santoso")
							 ->setLastModifiedBy("NuBuSa")
							 ->setTitle("Unis BTN Data")
							 ->setSubject("BTN")
							 ->setDescription("budhi_nusa@yahoo.com")
							 ->setKeywords("btn nubusa")
							 ->setCategory("Nubusa");

      $objPHPExcel->setActiveSheetIndex(0);
      
      $kode_institute = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_kode_institusi");
      $kode_pembayaran = $this->global_models->get_field("keuangan_mahasiswa_jenis_virtual_account", "code", array("id_keuangan_mahasiswa_jenis_virtual_account" => $id_keuangan_mahasiswa_jenis_virtual_account));
//      $objPHPExcel->getActiveSheet()->mergeCells('A1:C2');
      $objPHPExcel->getActiveSheet()->setCellValue('A1', 'Kode Institusi');
      $objPHPExcel->getActiveSheet()->setCellValue('A2', $kode_institute[1]);
      $objPHPExcel->getActiveSheet()->setCellValue('B1', 'Kode pembayaran');
      $objPHPExcel->getActiveSheet()->setCellValue('B2', $kode_pembayaran);
      $objPHPExcel->getActiveSheet()->setCellValue('C1', 'Tanggal File');
      $objPHPExcel->getActiveSheet()->setCellValue('C2', date("dmY"));
      $objPHPExcel->getActiveSheet()->setCellValue('D1', 'Jumlah Data');
      
      $objPHPExcel->getActiveSheet()->setCellValue('E1', 'Tahun');
      $objPHPExcel->getActiveSheet()->setCellValue('E2', date("Y"));
      $objPHPExcel->getActiveSheet()->setCellValue('F1', 'Bulan');
      $objPHPExcel->getActiveSheet()->setCellValue('F2', date("M"));
      
      $objPHPExcel->getActiveSheet()->setCellValue('A4', 'ID PELANGGAN');
      $objPHPExcel->getActiveSheet()->setCellValue('B4', 'NAMA');
      $objPHPExcel->getActiveSheet()->setCellValue('C4', 'FAKULTAS');
      $objPHPExcel->getActiveSheet()->setCellValue('D4', 'ID PRODI');
      $objPHPExcel->getActiveSheet()->setCellValue('E4', 'NAMA PRODI');
      $objPHPExcel->getActiveSheet()->setCellValue('F4', 'ANGKATAN');
      $objPHPExcel->getActiveSheet()->setCellValue('G4', 'REKENING GIRO');
      $objPHPExcel->getActiveSheet()->setCellValue('H4', 'REMARK');
      $objPHPExcel->getActiveSheet()->setCellValue('I4', 'TAGIHAN 01');
      $objPHPExcel->getActiveSheet()->setCellValue('J4', 'TAGIHAN 02');
      $objPHPExcel->getActiveSheet()->setCellValue('K4', 'TAGIHAN 03');
      $objPHPExcel->getActiveSheet()->setCellValue('L4', 'TAGIHAN 04');
      $objPHPExcel->getActiveSheet()->setCellValue('M4', 'TAGIHAN 05');
      $objPHPExcel->getActiveSheet()->setCellValue('N4', 'TAGIHAN 06');
      $objPHPExcel->getActiveSheet()->setCellValue('O4', 'TAGIHAN 07');
      $objPHPExcel->getActiveSheet()->setCellValue('P4', 'TAGIHAN 08');
      $objPHPExcel->getActiveSheet()->setCellValue('Q4', 'TAGIHAN 09');
      $objPHPExcel->getActiveSheet()->setCellValue('R4', 'KODE OPERATOR');
      $objPHPExcel->getActiveSheet()->setCellValue('S4', 'PIN');
      $objPHPExcel->getActiveSheet()->setCellValue('T4', 'METODE PAYMENT');
      
      $objPHPExcel->getActiveSheet()->getStyle('A4:T4')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
      
//      $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
//      $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
//      $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
      
//      $objPHPExcel->getActiveSheet()->setCellValue('A4', 'Name');
//      $objPHPExcel->getActiveSheet()->setCellValue('B4', 'Email');
//      $objPHPExcel->getActiveSheet()->setCellValue('C4', 'Status');
      $objPHPExcel->getActiveSheet()->getStyle('A4:T4')->applyFromArray(
          array(
            'font'    => array(
              'bold'      => true
            ),
            'alignment' => array(
              'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
            ),
            'borders' => array(
//              'top'     => array(
//                'style' => PHPExcel_Style_Border::BORDER_THIN
//              ),
//              'bottom'     => array(
//                'style' => PHPExcel_Style_Border::BORDER_THIN
//              ),
//              'left'     => array(
//                'style' => PHPExcel_Style_Border::BORDER_THIN
//              ),
//              'right'     => array(
//                'style' => PHPExcel_Style_Border::BORDER_THIN
//              ),
              'allborders'     => array(
                'style' => PHPExcel_Style_Border::BORDER_THIN
              ),
            ),
            'fill' => array(
              'type'       => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
//                'rotation'   => 90,
//              'startcolor' => array(
//                'argb' => 'FFFF99'
//              ),
//              'endcolor'   => array(
//                'argb' => 'FFFF99'
//              )
            )
          )
      );
      $t = count($mahasiswa) + 5;
      $objPHPExcel->getActiveSheet()->getStyle('A5:T'.$t)->applyFromArray(
          array(
            'font'    => array(
              'bold'      => false
            ),
            'alignment' => array(
              'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
            ),
            'borders' => array(
//              'top'     => array(
//                'style' => PHPExcel_Style_Border::BORDER_DASHDOT
//              ),
//              'bottom'     => array(
//                'style' => PHPExcel_Style_Border::BORDER_DASHDOT
//              ),
//              'left'     => array(
//                'style' => PHPExcel_Style_Border::BORDER_DASHDOT
//              ),
//              'right'     => array(
//                'style' => PHPExcel_Style_Border::BORDER_DASHDOT
//              ),
              'allborders'     => array(
                'style' => PHPExcel_Style_Border::BORDER_DASHDOT
              ),
            ),
            'fill' => array(
              'type'       => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
            )
          )
      );
      $objPHPExcel->getActiveSheet()->getStyle('A4:T4')->getFill()->getStartColor()->setARGB('FFFF99');
      $row = 5;
      $hitung = 0;
      foreach($mahasiswa AS $mhs){
        $tunggakan = $this->mfrm->get_tanggungan_jenis_virtual($id_keuangan_mahasiswa_jenis_virtual_account, $mhs->nim);
        if($tunggakan > 0){
          $objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $mhs->nim);
          $objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $mhs->nama);
          $objPHPExcel->getActiveSheet()->setCellValue('C'.$row, $mhs->fakultas);
  //        $objPHPExcel->getActiveSheet()->setCellValue('D4', 'ID PRODI');
          $objPHPExcel->getActiveSheet()->setCellValue('E'.$row, $mhs->prodi);
          $objPHPExcel->getActiveSheet()->setCellValue('F'.$row, $mhs->nama_kelompok);
  //        $objPHPExcel->getActiveSheet()->setCellValue('G4', 'REKENING GIRO');
  //        $objPHPExcel->getActiveSheet()->setCellValue('H4', 'REMARK');
          $objPHPExcel->getActiveSheet()->setCellValue('I'.$row, $tunggakan);
  //        $objPHPExcel->getActiveSheet()->setCellValue('J4', 'TAGIHAN 02');
  //        $objPHPExcel->getActiveSheet()->setCellValue('K4', 'TAGIHAN 03');
  //        $objPHPExcel->getActiveSheet()->setCellValue('L4', 'TAGIHAN 04');
  //        $objPHPExcel->getActiveSheet()->setCellValue('M4', 'TAGIHAN 05');
  //        $objPHPExcel->getActiveSheet()->setCellValue('N4', 'TAGIHAN 06');
  //        $objPHPExcel->getActiveSheet()->setCellValue('O4', 'TAGIHAN 07');
  //        $objPHPExcel->getActiveSheet()->setCellValue('P4', 'TAGIHAN 08');
  //        $objPHPExcel->getActiveSheet()->setCellValue('Q4', 'TAGIHAN 09');
  //        $objPHPExcel->getActiveSheet()->setCellValue('R4', 'KODE OPERATOR');
  //        $objPHPExcel->getActiveSheet()->setCellValue('S4', 'PIN');
          $objPHPExcel->getActiveSheet()->setCellValue('T4', 'F');
          $hitung++;
          $row++;
        }
      }
      $objPHPExcel->getActiveSheet()->setCellValue('D2', $hitung);
//      $data = $this->get();
//      if(is_array($data)){
//        foreach ($data as $key => $value) {
//          $objPHPExcel->getActiveSheet()->setCellValue('A'.(5+$key), $value->name);
//          $objPHPExcel->getActiveSheet()->setCellValue('B'.(5+$key), $value->email);
//          $objPHPExcel->getActiveSheet()->setCellValue('C'.(5+$key), $value->status);
//        }
//      }
//      $objPHPExcel->getActiveSheet()->getStyle('A5:C'.(5+$key))->applyFromArray(
//          array(
//            'font'    => array(
//              'bold'      => false
//            ),
//            'alignment' => array(
//              'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
//            ),
//            'borders' => array(
//              'bottom'     => array(
//                'style' => PHPExcel_Style_Border::BORDER_THIN
//              ),
//              'left'     => array(
//                'style' => PHPExcel_Style_Border::BORDER_THIN
//              ),
//              'right'     => array(
//                'style' => PHPExcel_Style_Border::BORDER_THIN
//              ),
//            ),
//            'fill' => array(
//              'type'       => PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR,
//                'rotation'   => 90,
//              'startcolor' => array(
//                'argb' => 'FFA0A0A0'
//              ),
//              'endcolor'   => array(
//                'argb' => 'FFFFFFFF'
//              )
//            )
//          )
//      );
      $abjad = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T');
      foreach ($abjad AS $colom){
        $objPHPExcel->getActiveSheet()->getColumnDimension($colom)->setAutoSize(true);
      }
      $objPHPExcel->getActiveSheet()->getStyle('A1:T4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
      $objPHPExcel->getActiveSheet()->getStyle('A1:T4')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
//      $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
//      $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(30);
//      $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(30);
//      $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(50);
//      $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
      
      $objPHPExcel->setActiveSheetIndex(0);
      header('Content-Type: application/vnd.ms-excel');
      header('Content-Disposition: attachment;filename="'.$filename."-".date("Y m d").'".xls');
      header('Cache-Control: max-age=0');
      $objWriter = PHPExcel_IOFactory::createWriter($this->phpexcel, 'Excel5');
//$objWriter->save(str_replace('.php', '.xls', __FILE__));
      $objWriter->save('php://output');die;
    }
    
    function import_btn_format(){
      $this->template->build("import-list-btn-format",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'keuangan-mahasiswa/mahasiswa',
              'title'       => "Import BTN",
              'data'        => $this->global_models->get("keuangan_mahasiswa_import_transaksi_btn"),
            ));
      $this->template
        ->set_layout('default')
        ->build("import-list-btn-format");
    }
    
    function add_import_btn_format(){
       
      if($_FILES['data']['name']){
//        $this->debug($_FILES, true);
        $config['upload_path'] = './files/keuangan-mahasiswa/btn/';
        $config['allowed_types'] = '*';
        
        $this->load->library('upload', $config);
        if($_FILES['data']['name']){
          if (  $this->upload->do_upload('data')){
            $data = array('upload_data' => $this->upload->data());
          }
          else{
            print $this->upload->display_errors();
            print "<br /> <a href='".site_url("keuangan-mahasiswa/add-import-btn-format")."'>Back</a>";
            die;
          }
          $kirim = array(
              "document"        => $data['upload_data']['file_name'],
              "status"          => 1,
              "create_by_users" => $this->session->userdata("id"),
              "create_date"     => date("Y-m-d H:i:s"),
          );
          $this->global_models->insert("keuangan_mahasiswa_import_transaksi_btn", $kirim);
          redirect("keuangan-mahasiswa/import-btn-format");
          
        }
        
      }
      else{
        $this->template->build("add-import-list-btn-format",
          array(
                'url'         => base_url()."themes/".DEFAULTTHEMES."/",
                'menu'        => 'keuangan-mahasiswa/mahasiswa',
                'title'       => "Import BTN",
              ));
        $this->template
          ->set_layout('default')
          ->build("add-import-list-btn-format");
      }
    }
    function eksekusi_import_transaksi_btn($id_keuangan_mahasiswa_import_transaksi_btn){
      $data = $this->global_models->get("keuangan_mahasiswa_import_transaksi_btn", 
              array("id_keuangan_mahasiswa_import_transaksi_btn" => $id_keuangan_mahasiswa_import_transaksi_btn, 
                  "status" => 1
                  ));
      
      if($data){
        $excel = new Spreadsheet_Excel_Reader("files/keuangan-mahasiswa/btn/".$data[0]->document);
        for($r = 1 ; $r <= 8 ; $r++){
          unset($excel->sheets[0]['cells'][$r]);
        }
        
        $keuangan_mahasiswa_vid_btn = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_vid_btn");
        $id_pembayaran_mahasiswa = $this->nbscache->get_explode("variable", "id_pembayaran_mahasiswa");
//        $id_piutang_mahasiswa = $this->nbscache->get_explode("variable", "id_piutang_mahasiswa");
        
        $id_hutang_mahasiswa = $this->nbscache->get_explode("variable", "id_hutang_mahasiswa");
        $jumlah_prefix = strlen($keuangan_mahasiswa_vid_btn[1]);
//        $this->debug($excel->sheets[0]['cells']);
        
        foreach($excel->sheets[0]['cells'] AS $cell){
          if($cell[2] == "Jumlah Total")
            break;
          if($cell[2] AND substr($cell[2], 0, $jumlah_prefix) == $keuangan_mahasiswa_vid_btn[1]){
            $price_temp = explode(".", $cell[19]);
            $price = str_replace(",", "", $price_temp[0]);
            
            $jam = substr($cell[17], 0, 2);
            if($jam > 24){
              $jam = "0".substr($cell[17], 0, 1);
              $posisitime = 1;
            }
            else{
              $posisitime = 2;
            }
            
            $menit = substr($cell[17], $posisitime, 2);
            if($menit > 60){
              $menit = "0".substr($cell[17], $posisitime, 1);
              $posisitime += 1;
            }
            else{
              $posisitime += 2;
            }
            
            $detik = substr($cell[17], $posisitime, 2);
            if(!$detik){
              $detik = "0".substr($cell[17], $posisitime, 1);
            }
            
            $this->global_models->get_connect("zikula");
            
            $mahasiswa = $this->global_models->get_query("
              SELECT A.course_category, A.users
              FROM academicportal_student_categories AS A
              LEFT JOIN academicportal_mahasiswa AS B ON A.users = B.pn_uid
              WHERE B.nim = '".substr($cell[2], $jumlah_prefix)."'");
            $fakultas = $this->global_models->get_field("academicportal_course_categories", "parent", array("id" => $mahasiswa[0]->course_category));
            
            $this->global_models->get_connect("default");
            $current = $this->get_current_sta($mahasiswa[0]->course_category);
            
            $id_keuangan_mahasiswa_jenis_virtual_account = $this->global_models->get_field("keuangan_mahasiswa_jenis_virtual_account", 
                    "id_keuangan_mahasiswa_jenis_virtual_account", array("code" => $cell[18]));
            
            $id_piutang_mahasiswa = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_fakultas_beban_mahasiswa_".$fakultas);
            
            $this->global_models->get_connect("zikula");
            $program_kuliah = $this->global_models->get("academicportal_mahasiswa", array("nim" => trim(substr($cell[2], $jumlah_prefix))));
            $this->global_models->get_connect("default");
            
            $kirim = array(
                "nim"                 => trim(substr($cell[2], $jumlah_prefix)),
                "id_keuangan_mahasiswa_jenis_virtual_account" => $id_keuangan_mahasiswa_jenis_virtual_account,
                "id_frm_account"      => $id_pembayaran_mahasiswa[1],
                "id_program_kuliah"   => $program_kuliah[0]->program_kuliah,
                "id_kelompok"         => $program_kuliah[0]->kelompok_mahasiswa,
                "notransaksi"         => $cell[18],
                "saldo"               => $price,
                "pos"                 => 2,
                "id_semester"         => $current[0]->semester,
                "tahun"               => $current[0]->tahun_ajaran,
                "tahunsemester"       => $current[0]->tahun_ajaran.$current[0]->semester,
                "note"                => "Pembayaran Via BTN",
                "create_by_users"     => $this->session->userdata("id"),
                "create_date"         => date("Y-m-d", strtotime(str_replace("/", "-", $cell[15])))." {$jam}:{$menit}:{$detik}",
            );
            
            $tunggakan = $this->mfrm->get_tunggakan(trim(substr($cell[2], $jumlah_prefix)));
            
            $tunggakan_setelah_bayar = $tunggakan - $price;
            
            if($tunggakan <= 0){
              $price_piutang += $price;
            }
            else{
              if($tunggakan >= $price){
                $price_hutang += $price;
              }
              else{
                $price_hutang += $tunggakan;
                $price_piutang += ($price - $tunggakan);
              }
            }
            
//            $this->debug($tunggakan);
            
//            if($tunggakan_setelah_bayar < 0 AND $tunggakan > 0){
//              $price_piutang += ($tunggakan_setelah_bayar * -1);
//            }
//            else if($tunggakan_setelah_bayar < 0){
//              $price_piutang += $price;
//            }
//            else{
//              $price_hutang += $price;
//            }
            $id_keuangan_mahasiswa_tanggungan = $this->global_models->insert("keuangan_mahasiswa_tanggungan", $kirim);
            
            //        cek lunas
          $total_debit = $this->global_models->get_query("
            SELECT SUM(A.saldo) AS saldo
            FROM keuangan_mahasiswa_tanggungan AS A
            WHERE A.nim = '".trim(substr($cell[2], $jumlah_prefix))."' AND A.pos = 1
            ");

          $total_kredit = $this->global_models->get_query("
            SELECT SUM(A.saldo) AS saldo
            FROM keuangan_mahasiswa_tanggungan AS A
            WHERE A.nim = '".trim(substr($cell[2], $jumlah_prefix))."' AND (A.pos = 2 OR A.pos = 3)
            ");

//          $pos_asli = $this->global_models->get_field("frm_account", "pos", array("id_frm_account" => $id_frm_account));
//          if($pos_asli == 1)
//            $total_akhir = $total_debit[0]->saldo - $total_kredit[0]->saldo;
//          else
            $total_akhir = $total_kredit[0]->saldo - $total_debit[0]->saldo;  

          if($total_akhir >= 0){
  //          set keuangan
            $this->global_models->get_connect("zikula");
            $this->global_models->update("academicportal_persyaratan_keuangan", array("uid" => $mahasiswa[0]->users),
                    array("delete_by_who" => -1));
            $this->global_models->get_connect("default");
          }
                
            $price_all += $price;
          }
//          $this->debug(substr($cell[2], $jumlah_prefix));
          
          
          

        }
//        $this->debug($price_all);
//        if($kirim){
//          $this->global_models->insert_batch("keuangan_mahasiswa_tanggungan", $kirim);
//        }
        
        $kirim_journal = array(
            "id_frm_journal"      => $this->mfrm->get_journal_current(),
            "notransaksi"         => $cell[18],
            "saldo"               => $price_all,
            "tanggal"             => date("Y-m-d"),
            "note"                => "Transaksi Pembayaran Via BTN",
        );
            
        $id_frm_journal_log = $this->mfrm->create_journal_log($kirim_journal);
        if($id_frm_journal_log){
          $debit[] = $price_all;
          $debit_da[] = $id_pembayaran_mahasiswa[1];

          $kredit[] = $price_hutang;
          $kredit_da[] = $id_piutang_mahasiswa[1];
          
          $kredit[] = $price_piutang;
          $kredit_da[] = $id_hutang_mahasiswa[1];
          
          $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);
        }
      }
      
      $this->global_models->update("keuangan_mahasiswa_import_transaksi_btn", 
              array("id_keuangan_mahasiswa_import_transaksi_btn" => $id_keuangan_mahasiswa_import_transaksi_btn), array("status" => 2));
      
      redirect("keuangan-mahasiswa/import-btn-format");
    }
    
  public function setting_pembayaran_beban_mahasiswa(){
    if($this->input->post()){
      $pst = $this->input->post();
      foreach($pst["keuangan_mahasiswa_fakultas_beban_mahasiswa"] AS $id_fakultas => $id_frm_account){
        $this->nbscache->put_tunggal("variable", "keuangan_mahasiswa_fakultas_beban_mahasiswa_".$id_fakultas, $id_frm_account);
      }
    }
    $this->global_models->get_connect("zikula");
    $data = $this->global_models->get_query("
      SELECT name, shortname, id
      FROM academicportal_course_categories
      WHERE set_as_fakultas = 1
      ORDER BY sortorder ASC
      ");
    $this->global_models->get_connect("default");
    $this->template->build("setting-pembayaran-beban-mahasiswa",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-mahasiswa/setting-pembayaran-beban-mahasiswa',
            'title'       => lang("keuangan_mahasiswa_setting_prodi_beban"),
            'data'        => $data
          ));
    $this->template
      ->set_layout('default')
      ->build("setting-pembayaran-beban-mahasiswa");
	}
  
  function kas_mahasiswa_detail($nim){
    $data = $this->global_models->get_query("
      SELECT A.*
      FROM keuangan_mahasiswa_jenis_virtual_account AS A
      ");
    $this->template->build("kas-mahasiswa-detail",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-mahasiswa/mahasiswa',
            'title'       => lang("keuangan_mahasiswa_kas_mahasiswa_detail"),
            'data'        => $data,
            'nim'         => $nim
          ));
    $this->template
      ->set_layout('default')
      ->build("kas-mahasiswa-detail");
  }
  
  function kas_mahasiswa_baru_detail($id_ao_applicant){
    $data = $this->global_models->get_query("
      SELECT A.*
      FROM keuangan_mahasiswa_jenis_virtual_account AS A
      ");
    $this->template->build("kas-mahasiswa-baru-detail",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-mahasiswa/mahasiswa',
            'title'       => lang("keuangan_mahasiswa_kas_mahasiswa_baru_detail"),
            'data'        => $data,
            'id_ao_applicant'         => $id_ao_applicant
          ));
    $this->template
      ->set_layout('default')
      ->build("kas-mahasiswa-baru-detail");
  }
  
  function get_dropdown_sta($id_program, $tru = 0){
    $this->global_models->get_connect("zikula");
    $sta_dropdown_temp = $this->global_models->get_query("
      SELECT A. semester, A.tahun_ajaran, B.semester_name
      FROM academicportal_semester_tahun_ajaran AS A
      LEFT JOIN academicportal_semester AS B ON A.semester = B.id
      WHERE program_kuliah = {$id_program}");
    
    if($tru == 2){
      $sta_dropdown[""] = "- Pilih -";
    }
    
    foreach($sta_dropdown_temp AS $sdt){
      $sta_dropdown[$sdt->tahun_ajaran.$sdt->semester] = $sdt->semester_name." - ".$sdt->tahun_ajaran."/".($sdt->tahun_ajaran + 1);
    }
    $this->global_models->get_connect("default");
    print $this->form_eksternal->form_dropdown('cari_sta', $sta_dropdown, "");die;
  }
  
  function bayar_biaya_formulir_mahasiswa_baru($no_ref){
    
    if($this->input->post()){
      $pst = $this->input->post();
      
      $id_pendapatan_formulir_pendaftaran = $this->nbscache->get_explode("variable", "id_pendapatan_formulir_pendaftaran");
      $id_formulir_pendaftaran = $this->nbscache->get_explode("variable", "id_formulir_pendaftaran");
      $keuangan_mahasiswa_formulir_pendaftaran = $this->nbscache->get_explode("variable", "keuangan_mahasiswa_formulir_pendaftaran");
      
      $kirim = array(
              "id_frm_journal"      => $this->mfrm->get_journal_current(),
              "notransaksi"         => $pst['notransaksi'],
              "saldo"               => $keuangan_mahasiswa_formulir_pendaftaran[1],
              "tanggal"             => $pst['tanggal'],
              "note"                => "Formulir Pendaftaran <a href='".site_url("keuangan-mahasiswa/mahasiswa-baru/{$pst['no_ref']}")."'>{$pst['no_ref']}</a>",
          );

      $id_frm_journal_log = $this->mfrm->create_journal_log($kirim);
      if($id_frm_journal_log){ 
        $debit[] = $keuangan_mahasiswa_formulir_pendaftaran[1];
        $debit_da[] = $id_formulir_pendaftaran[1];

        $kredit[] = $keuangan_mahasiswa_formulir_pendaftaran[1];
        $kredit_da[] = $id_pendapatan_formulir_pendaftaran[1];
        $this->mfrm->create_journal_log_account($this->mfrm->get_journal_current(), $id_frm_journal_log, $debit, $debit_da, $kredit, $kredit_da);
      }
      
      $this->global_models->get_connect("zikula");
      $this->global_models->update("academicportal_ao_applicant", array("ref_number" => $pst['no_ref']), array("status_pembayaran_formulir" => 1));
      $this->global_models->get_connect("default");

      redirect("keuangan-mahasiswa/mahasiswa-baru");
    }
    else{
      $this->template->build("biaya-formulir-mahasiswa",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'keuangan-mahasiswa/mahasiswa-baru',
              'title'       => "Biaya Formulir Mahasiswa",
              'no_ref'      => $no_ref
            ));
      $this->template
        ->set_layout('default')
        ->build("biaya-formulir-mahasiswa");
    }
  }
  
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */